We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 226fc3d commit d98a784Copy full SHA for d98a784
Python/flip-equivalent-binary-trees.py
@@ -0,0 +1,26 @@
1
+# Time: O(n)
2
+# Space: O(h)
3
+
4
+# Definition for a binary tree node.
5
+class TreeNode(object):
6
+ def __init__(self, x):
7
+ self.val = x
8
+ self.left = None
9
+ self.right = None
10
11
+class Solution(object):
12
+ def flipEquiv(self, root1, root2):
13
+ """
14
+ :type root1: TreeNode
15
+ :type root2: TreeNode
16
+ :rtype: bool
17
18
+ if not root1 and not root2:
19
+ return True
20
+ if not root1 or not root2 or root1.val != root2.val:
21
+ return False
22
23
+ return (self.flipEquiv(root1.left, root2.left) and
24
+ self.flipEquiv(root1.right, root2.right) or
25
+ self.flipEquiv(root1.left, root2.right) and
26
+ self.flipEquiv(root1.right, root2.left))
0 commit comments