Skip to content

Commit 3a1db7e

Browse files
committed
LC 951. Flip Equivalent Binary Trees (Python)
1 parent 1e8228d commit 3a1db7e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ to the solution in this repository.
432432
| [947. Most Stones Removed with Same Row or Column][lc947] | 🟠 Medium | [![python](res/py.png)][lc947py] |
433433
| [948. Bag of Tokens][lc948] | 🟠 Medium | [![python](res/py.png)][lc948py] [![rust](res/rs.png)][lc948rs] |
434434
| [950. Reveal Cards In Increasing Order][lc950] | 🟠 Medium | [![rust](res/rs.png)][lc950rs] |
435+
| [951. Flip Equivalent Binary Trees][lc951] | 🟠 Medium | [![python](res/py.png)][lc951py] |
435436
| [953. Verifying an Alien Dictionary][lc953] | 🟢 Easy | [![python](res/py.png)][lc953py] [![rust](res/rs.png)][lc953rs] |
436437
| [956. Tallest Billboard][lc956] | 🟠 Medium | [![python](res/py.png)][lc956py] |
437438
| [958. Check Completeness of a Binary Tree][lc958] | 🟠 Medium | [![python](res/py.png)][lc958py] [![rust](res/rs.png)][lc958rs] |
@@ -1720,6 +1721,8 @@ to the solution in this repository.
17201721
[lc948rs]: leetcode/bag-of-tokens.rs
17211722
[lc950]: https://leetcode.com/problems/reveal-cards-in-increasing-order/
17221723
[lc950rs]: leetcode/reveal-cards-in-increasing-order.rs
1724+
[lc951]: https://leetcode.com/problems/flip-equivalent-binary-trees/
1725+
[lc951py]: leetcode/flip-equivalent-binary-trees.py
17231726
[lc953]: https://leetcode.com/problems/verifying-an-alien-dictionary/
17241727
[lc953py]: leetcode/verifying-an-alien-dictionary.py
17251728
[lc953rs]: leetcode/verifying-an-alien-dictionary.rs
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 951. Flip Equivalent Binary Trees
2+
# 🟠 Medium
3+
#
4+
# https://leetcode.com/problems/flip-equivalent-binary-trees/
5+
#
6+
# Tags: Tree - Depth-First Search - Binary Tree
7+
8+
import timeit
9+
from typing import Optional
10+
11+
from utils.binary_tree import BinaryTree, TreeNode
12+
13+
14+
# Use recursion, check that the values at the current node match, if
15+
# they do not, we can return false. If they do match, we need to explore
16+
# the right and left subtrees recursively both flipping them and keeping
17+
# them in their original form.
18+
#
19+
# Time complexity: O(n) - At every node, we will check two branches,
20+
# flipping the children and not flipping the children, but one of them
21+
# will fail immediately O(1) because values are unique, otherwise, it
22+
# would be O(2^n)
23+
# Space complexity: O(h) - Where h is the height of the tree, that is
24+
# the space used by the call stack.
25+
#
26+
# Runtime 0 ms Beats 100%
27+
# Memory 16.58 MB Beats 57%
28+
class Solution:
29+
def flipEquiv(
30+
self, root1: Optional[TreeNode], root2: Optional[TreeNode]
31+
) -> bool:
32+
if not root1 or not root2:
33+
return not root1 and not root2
34+
return root1.val == root2.val and (
35+
(
36+
self.flipEquiv(root1.left, root2.right)
37+
and self.flipEquiv(root1.right, root2.left)
38+
)
39+
or (
40+
self.flipEquiv(root1.left, root2.left)
41+
and self.flipEquiv(root1.right, root2.right)
42+
)
43+
)
44+
45+
46+
def test():
47+
executors = [Solution]
48+
tests = [
49+
[[], [], True],
50+
[[], [1], False],
51+
[
52+
[1, 2, 3, 4, 5, 6, None, None, None, 7, 8],
53+
[1, 3, 2, None, 6, 4, 5, None, None, None, None, 8, 7],
54+
True,
55+
],
56+
]
57+
for executor in executors:
58+
start = timeit.default_timer()
59+
for _ in range(1):
60+
for col, t in enumerate(tests):
61+
sol = executor()
62+
result = sol.flipEquiv(
63+
BinaryTree.fromList(t[0]).getRoot(),
64+
BinaryTree.fromList(t[1]).getRoot(),
65+
)
66+
exp = t[2]
67+
assert result == exp, (
68+
f"\033[93m» {result} <> {exp}\033[91m for"
69+
+ f" test {col} using \033[1m{executor.__name__}"
70+
)
71+
stop = timeit.default_timer()
72+
used = str(round(stop - start, 5))
73+
cols = "{0:20}{1:10}{2:10}"
74+
res = cols.format(executor.__name__, used, "seconds")
75+
print(f"\033[92m» {res}\033[0m")
76+
77+
78+
test()

0 commit comments

Comments
 (0)