-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path222_CountCompleteTreeNodes.py
More file actions
46 lines (42 loc) · 1.18 KB
/
222_CountCompleteTreeNodes.py
File metadata and controls
46 lines (42 loc) · 1.18 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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def countNodes(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def helper(node):
if not node:
return 0
return 1+helper(node.left)
if not root:
return 0
leftd = helper(root.left)
rightd = helper(root.right)
if leftd == rightd:
return pow(2, leftd) + self.countNodes(root.right)
else:
return pow(2, rightd) + self.countNodes(root.left)
def countNodes_iter(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def height(node):
return -1 if not node else 1+height(node.left)
h = height(root)
nodes = 0
while root:
if height(root.right) == h-1:
nodes += 2**h
root = root.right
else:
nodes += 2 ** (h-1)
root = root.left
h -= 1
return nodes