-
Notifications
You must be signed in to change notification settings - Fork 0
/
222-count-complete-tree-nodes.py
61 lines (51 loc) · 1.47 KB
/
222-count-complete-tree-nodes.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
"""
Given a complete binary tree, count the number of nodes.
Note:
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6
Output: 6
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def getHeight(self, node):
if not node:
return 0
return 1 + self.getHeight(node.left)
def countNodes(self, root):
"""
:type root: TreeNode
:rtype: int
time complexity: O(log(N)^2)
"""
cnt = 0
while root:
# root is height 0
l, r = self.getHeight(root.left), self.getHeight(root.right)
if l == r:
root = root.right
# add the number of left subtree nodes
cnt += pow(2, l)
else:
root = root.left
# add the number of left subtree nodes without last level
cnt += pow(2, r)
return cnt
if __name__ == "__main__":
vals = [1, 2, 3]
root = TreeNode(vals[0])
node = root
node.left = TreeNode(vals[1])
node.right = TreeNode(vals[2])
print(Solution().countNodes(root))