-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary-tree-pruning.py
98 lines (87 loc) · 3.19 KB
/
binary-tree-pruning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 814. Binary Tree Pruning
# 🟠 Medium
#
# https://leetcode.com/problems/binary-tree-pruning/
#
# Tags: Tree - Depth-First Search - Binary Tree
import timeit
from typing import Optional
from data import (
TreeNode,
deserializeStringArrayToBinaryTree,
serializeBinaryTreeToStringArray,
)
# We can use postorder DFS, each subtree returns whether it contains or
# not a 1, the parent prunes any child subtree that does not contain a 1.
#
# Time complexity: O(n) - We visit each node once.
# Space complexity: O(n) - For the call stack.
#
# Runtime: 62 ms, faster than 19.19%
# Memory Usage: 13.7 MB, less than 97.77%
class RecursiveDFS:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
# Base case.
if not root:
return None
# DFS function returns whether the child contains a 1.
def containsOne(node: TreeNode) -> bool:
# Unlink the children if they exist and do not contain a 1.
if node.left and not containsOne(node.left):
node.left = None
if node.right and not containsOne(node.right):
node.right = None
# If this node val is 1 or has either child, there is a 1.
return node.val or node.left or node.right
# Check if we need to prune the entire tree.
if not containsOne(root):
return None
return root
# Making the observation that we are really just recursively pruning
# the left and right subtrees, we can eliminate the helper function and
# recursively call `pruneTree`.
#
# Time complexity: O(n) - We visit each node once.
# Space complexity: O(n) - For the call stack.
#
# Runtime: 52 ms, faster than 46.04%
# Memory Usage: 13.7 MB, less than 100.00%
class RecursiveDFS2:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
# Base case.
if not root:
return None
# Recursively prune left and right subtrees.
root.left = self.pruneTree(root.left)
root.right = self.pruneTree(root.right)
# Return the root if this subtree contains a 1 somewhere.
return root if root.left or root.right or root.val else None
def test():
executors = [
RecursiveDFS,
RecursiveDFS2,
]
tests = [
["[0,null,0,0,0]", "[]"],
["[1,null,0,0,1]", "[1,null,0,null,1]"],
["[1,0,1,0,0,0,1]", "[1,null,1,null,1]"],
["[1,1,0,1,1,0,1,0]", "[1,1,0,1,1,null,1]"],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
root = deserializeStringArrayToBinaryTree(t[0])
result = serializeBinaryTreeToStringArray(sol.pruneTree(root))
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()