Skip to content

Commit 735cc0c

Browse files
author
Partho Biswas
committed
951_Flip_Equivalent_Binary_Trees
1 parent 6cda968 commit 735cc0c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ I have solved quite a number of problems from several topics. See the below tabl
264264
|14| [895. Maximum Frequency Stack](https://tinyurl.com/szdk34w) | [Python](https://tinyurl.com/wu6rdaw/895_Maximum_Frequency_Stack.py)| [educative.io](https://tinyurl.com/w5cd6yh) | Hard | 📌 TODO: Check again |
265265
|15| **[378. Kth Smallest Element in a Sorted Matrix](https://tinyurl.com/shz4289)** | [Python](https://tinyurl.com/wu6rdaw/378_Kth_Smallest_Element_in_a_Sorted_Matrix.py)| [educative.io](https://tinyurl.com/scbjgjd) | Hard | 📌 TODO: Check again the Binary Search approach. Very important |
266266
|16| **[632. Smallest Range Covering Elements from K Lists](https://tinyurl.com/v3fshwo)** | [Python](https://tinyurl.com/wu6rdaw/632_Smallest_Range_Covering_Elements_from_K_Lists.py)| [educative.io](https://tinyurl.com/ubfua7f) | Hard | 📌 TODO: Check again. Very important |
267-
|17| **[846. Hand of Straights](https://tinyurl.com/y2hfqqsv)** | [Python](https://tinyurl.com/wu6rdaw/846_Hand_of_Straights.py)| **[Art 1](https://tinyurl.com/tp46c6s)** | Hard | 📌 TODO: Check again. Very important |
267+
|17| **[846. Hand of Straights](https://tinyurl.com/y2hfqqsv)** | [Python](https://tinyurl.com/wu6rdaw/846_Hand_of_Straights.py)| **[Art 1](https://tinyurl.com/w8pkf55)** | Medium | 📌 TODO: Check again. Very important |
268268

269269
</p>
270270
</details>
@@ -340,6 +340,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
340340
|25| [257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Python](https://tinyurl.com/wu6rdaw/257_Binary_Tree_Paths.py)| --- | Easy | DFS |
341341
|26| **[1145. Binary Tree Coloring Game](https://tinyurl.com/uukf2nm)**| [Python](https://tinyurl.com/wu6rdaw/1145_Binary_Tree_Coloring_Game.py)| [Art 1](https://tinyurl.com/v4k7jaj), [Art 2](https://tinyurl.com/rvwsjar) | Medium | DFS |
342342
|27| **[298. Binary Tree Longest Consecutive Sequence](https://tinyurl.com/rw2sqxr)**| [Python](https://tinyurl.com/wu6rdaw/298_Binary_Tree_Longest_Consecutive_Sequence.py)| **[Art 1](https://tinyurl.com/wmlq6md)** | Medium | DFS |
343+
|28| **[951. Flip Equivalent Binary Trees](https://tinyurl.com/wj4bqms)**| [Python](https://tinyurl.com/wu6rdaw/951_Flip_Equivalent_Binary_Trees.py)| **[Vid 1](https://tinyurl.com/uw378m8)**, [Art 1](https://tinyurl.com/r5d6jkk) | Medium | DFS |
343344

344345
</p>
345346
</details>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Definition for a binary tree node.
2+
class TreeNode(object):
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
class Solution(object):
9+
def flipEquiv(self, root1, root2):
10+
"""
11+
:type root1: TreeNode
12+
:type root2: TreeNode
13+
:rtype: bool
14+
"""
15+
if not root1 and not root2:
16+
return True
17+
if not root1 or not root2:
18+
return False
19+
if root1.val != root2.val:
20+
return False
21+
22+
if not self.sameChildren(root1, root2):
23+
temp = root1.left
24+
root1.left = root1.right
25+
root1.right = temp
26+
return self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right)
27+
28+
29+
def sameChildren(self, root1, root2):
30+
sameLeft, sameRight = False, False
31+
if not root1.left and not root2.left:
32+
sameLeft = True
33+
elif not root1.left or not root2.left:
34+
sameLeft = False
35+
else:
36+
sameLeft = root1.left.val == root2.left.val
37+
38+
if not root1.right and not root2.right:
39+
sameRight = True
40+
elif not root1.right or not root2.right:
41+
sameRight = False
42+
else:
43+
sameRight = root1.right.val == root2.right.val
44+
45+
return sameLeft and sameRight
46+
47+

0 commit comments

Comments
 (0)