forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_204.py
45 lines (38 loc) · 807 Bytes
/
problem_204.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
class Node:
def __init__(self):
self.left = None
self.right = None
def count_nodes(root, lspine=0, rspine=0):
if not root:
return 0
if not lspine:
node = root
while node:
node = node.left
lspine += 1
if not rspine:
node = root
while node:
node = node.right
rspine += 1
if lspine == rspine:
return 2**lspine - 1
return 1 + \
count_nodes(root.left, lspine=lspine-1) + \
count_nodes(root.right, rspine=rspine-1)
# Tests
a = Node()
b = Node()
c = Node()
a.left = b
a.right = c
assert count_nodes(a) == 3
d = Node()
b.left = d
assert count_nodes(a) == 4
e = Node()
b.right = e
assert count_nodes(a) == 5
f = Node()
c.left = f
assert count_nodes(a) == 6