-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path814.py
108 lines (89 loc) · 3.15 KB
/
814.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
99
100
101
102
103
104
105
106
107
108
# [ LeetCode ] 814. Binary Tree Pruning
def solution(root: "TreeNode") -> "TreeNode":
def preorder_traverse(node: TreeNode) -> None | bool:
if node:
if not preorder_traverse(node=node.left):
node.left = None
if not preorder_traverse(node=node.right):
node.right = None
return node.val or node.left or node.right
return root if preorder_traverse(node=root) else None
def another_solution(root: "TreeNode") -> "TreeNode":
if not root:
return None
else:
if not another_solution(root=root.left):
root.left = None
if not another_solution(root=root.right):
root.right = None
if not root.val and not root.left and not root.right:
return None
else:
return root
if __name__ == "__main__":
class TreeNode:
def __init__(
self,
val: int,
left: "TreeNode" = None,
right: "TreeNode" = None
) -> None:
self.val = val
self.left = left
self.right = right
def create_binary_tree(index: int, items: list[int | None]) -> TreeNode:
if items[index] != None:
root: TreeNode = TreeNode(val=items[index])
if (index * 2 + 1) < len(items):
root.left = create_binary_tree(index=index*2+1, items=items)
if (index * 2 + 2) < len(items):
root.right = create_binary_tree(index=index*2+2, items=items)
return root
else:
return None
def convert_binary_tree_to_list(node: TreeNode):
global nodes
if node:
nodes.append(node.val)
if node.left or node.right:
if node.left:
convert_binary_tree_to_list(node.left)
else:
nodes.append(None)
if node.right:
convert_binary_tree_to_list(node.right)
else:
nodes.append(None)
cases: list[dict[str, dict[str, list[int | None]] | list[int | None]]] = [
{
"input": {
"items": [1, None, 0, None, None, 0, 1]
},
"output": [1, None, 0, None, 1]
},
{
"input": {
"items": [1, 0, 1, 0, 0, 0, 1]
},
"output": [1, None, 1, None, 1]
},
{
"input": { "items": [0] },
"output": []
}
]
for case in cases:
nodes: list[int | None] = []
convert_binary_tree_to_list(
node=solution(
root=create_binary_tree(index=0, items=case["input"]["items"])
)
)
assert case["output"] == nodes
nodes: list[int | None] = []
convert_binary_tree_to_list(
node=another_solution(
root=create_binary_tree(index=0, items=case["input"]["items"])
)
)
assert case["output"] == nodes