-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path222_count_complete_tree_nodes.py
More file actions
54 lines (48 loc) · 1.39 KB
/
Copy path222_count_complete_tree_nodes.py
File metadata and controls
54 lines (48 loc) · 1.39 KB
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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from binarytree import buildtree, inorder_tree_print
class Solution(object):
def leftHeight(self, root):
height = 0
while root is not None:
height += 1
root = root.left
return height
def rightHeight(self, root):
height = 0
while root is not None:
height += 1
root = root.right
return height
def countNodes(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
if root is None:
return 0
# optimization
lh = self.leftHeight(root)
rh = self.rightHeight(root)
if lh == rh:
return 2**lh - 1
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
vectors = [
None, 0,
[1], 1,
[1,2,3,4,5,6], 6,
[x * pow(-1,x) for x in range(16)], 16
]
for i in range(0, len(vectors), 2):
a = vectors[i]
expected = vectors[i+1]
tree = buildtree(a)
print(f'{a} {expected}')
inorder_tree_print(tree)
print('')
returned = Solution().countNodes(tree)
assert expected == returned, f'for {a} expected {expected}, but returned {returned}!'