forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Complete Tree Nodes.py
43 lines (34 loc) Β· 1.24 KB
/
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
# https://leetcode.com/problems/count-complete-tree-nodes/
# https://youtu.be/u-yWemKGWO0
'''
Only traversing along the left and right boundary.
If both left and right height are equal then
the bottom level would be full from left to right and total no. of nodes in that subtree
is 2^h - 1.
If left and right height are not equal then add +1 for current root and go to left child
and right child.
'''
class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root: return 0
leftHeight = self.getLeftHeight(root)
rightHeight = self.getRightHeight(root)
if leftHeight == rightHeight:
return 2 ** leftHeight - 1
else:
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
def getLeftHeight(self, node):
height = 0
while node:
height += 1
node = node.left
return height
def getRightHeight(self, node):
height = 0
while node:
height += 1
node = node.right
return height
# Height = H = log(n) where n = total number of nodes
# Time: O(H * H) = O(log(n)^2) = O(Log^2 n)
# Auxiliary Space: O(H) = O(log(n))