-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCompleteTreeNodes.py
More file actions
executable file
·77 lines (54 loc) · 1.92 KB
/
CountCompleteTreeNodes.py
File metadata and controls
executable file
·77 lines (54 loc) · 1.92 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Given the root of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, 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.
Design an algorithm that runs in less than O(n) time complexity.
Example 1:
Input: root = [1,2,3,4,5,6]
Output: 6
Example 2:
Input: root = []
Output: 0
Example 3:
Input: root = [1]
Output: 1
Constraints:
The number of nodes in the tree is in the range [0, 5 * 104].
0 <= Node.val <= 5 * 104
The tree is guaranteed to be complete.
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root:
return 0
left_height = self.get_height(root.left)
right_height = self.get_height(root.right)
if left_height == right_height:
# Left subtree is perfect, use the formula to calculate total nodes
return 2 ** left_height + self.countNodes(root.right)
else:
# Left subtree is not perfect, recursively count nodes in left and right subtrees
return 2 ** right_height + self.countNodes(root.left)
def get_height(self, node):
height = 0
while node:
height += 1
node = node.left
return height
# Example 1
root1 = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6)))
solution = Solution()
print(solution.countNodes(root1)) # Output should be 6
# Example 2
root2 = None
solution = Solution()
print(solution.countNodes(root2)) # Output should be 0
# Example 3
root3 = TreeNode(1)
solution = Solution()
print(solution.countNodes(root3)) # Output should be 1