-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path222.py
More file actions
55 lines (54 loc) · 1.57 KB
/
222.py
File metadata and controls
55 lines (54 loc) · 1.57 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
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):
if root is None:
return 0
h=0
head=root
while head:
h+=1
head=head.left
ans=[0]
def dfs(root,height, h, ans):
if root.left is None and root.right is None and height==h:
return
if root.left is not None and root.right is None and height==h-1:
ans[0]+=1
elif root.left is None and root.right is None and height==h-1:
ans[0]+=2
else:
height+=1
if root.left is not None and root.right is not None and height==h:
return
else:
dfs(root.right, height, h, ans)
dfs(root.left, height, h, ans)
dfs(root, 1, h, ans)
return pow(2, h)-ans[0]-1
def construct_tree(lst):
if lst is None:
return
head=TreeNode(lst[0])
queue=[]
queue.append(head)
i=0
while i<len(lst):
ele=queue.pop(0)
i=i+1
if i<len(lst) and lst[i] is not None:
new=TreeNode(lst[i])
queue.append(new)
ele.left=new
i=i+1
if i<len(lst) and lst[i] is not None:
new=TreeNode(lst[i])
queue.append(new)
ele.right=new
return head
root = [1,2,3,4,5,6,7,8]
l=construct_tree(root)
print(Solution().countNodes(l))