Skip to content

Commit d98a784

Browse files
authored
Create flip-equivalent-binary-trees.py
1 parent 226fc3d commit d98a784

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)