Skip to content

Commit 42f5421

Browse files
committed
951
1 parent 947fd39 commit 42f5421

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Oct-24-24.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
3+
if not root1 and not root2:
4+
return True
5+
elif not root1 or not root2:
6+
return False
7+
childMap1 = defaultdict(set)
8+
childMap2 = defaultdict(set)
9+
q1 = deque()
10+
q2 = deque()
11+
q1.append(root1)
12+
q2.append(root2)
13+
while q1 and q2:
14+
n1,n2 = q1.popleft(),q2.popleft()
15+
child1,child2 = set(),set()
16+
if n1.left:
17+
child1.add(n1.left.val)
18+
q1.append(n1.left)
19+
if n1.right:
20+
child1.add(n1.right.val)
21+
q1.append(n1.right)
22+
if n2.left:
23+
child2.add(n2.left.val)
24+
q2.append(n2.left)
25+
if n2.right:
26+
child2.add(n2.right.val)
27+
q2.append(n2.right)
28+
childMap1[n1.val] = child1
29+
childMap2[n2.val] = child2
30+
if q1 or q2 or childMap1.keys()!=childMap2.keys():
31+
return False
32+
for k in childMap1.keys():
33+
if childMap1[k]!=childMap2[k]:
34+
return False
35+
return True

0 commit comments

Comments
 (0)