-
Notifications
You must be signed in to change notification settings - Fork 0
/
008.py
89 lines (69 loc) · 2.07 KB
/
008.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
"""
Problem:
A unival tree (which stands for "universal value") is a tree where all nodes under it
have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0
/ \
1 0
/ \
1 0
/ \
1 1
"""
from typing import Tuple
from DataStructures.Tree import Node, BinaryTree
def num_universal_helper(node: Node, val: int, acc: int = 0) -> Tuple[int, bool]:
# base case for recursion [leaf node]
if node.left is None and node.right is None:
if node.val == val:
return (acc + 1), True
return (acc + 1), False
# if the value matches the parent's value, its children are also checked
elif node.val == val:
if node.left:
acc, res1 = num_universal_helper(node.left, val, acc)
else:
res1 = True
if node.right:
acc, res2 = num_universal_helper(node.right, val, acc)
else:
res2 = True
if res1 and res2:
acc += 1
# If the value doesn't match the parent's value, its children are checked with the
# new value (value of the current node)
else:
if node.left:
acc, res1 = num_universal_helper(node.left, node.val, acc)
else:
res1 = True
if node.right:
acc, res2 = num_universal_helper(node.right, node.val, acc)
else:
res2 = True
if res1 and res2:
acc += 1
return acc, (node.val == val)
def num_universal(tree: BinaryTree) -> int:
if not tree.root:
return 0
result, _ = num_universal_helper(tree.root, tree.root.val)
return result
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node(0)
tree.root.left = Node(1)
tree.root.right = Node(0)
tree.root.right.left = Node(1)
tree.root.right.right = Node(0)
tree.root.right.left.left = Node(1)
tree.root.right.left.right = Node(1)
print(tree)
print(num_universal(tree))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n)) [call stack]
"""