forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_perfect_binary_tree.py
More file actions
38 lines (27 loc) · 883 Bytes
/
check_perfect_binary_tree.py
File metadata and controls
38 lines (27 loc) · 883 Bytes
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
# A binary tree is perfect if all the internal nodes have 2 children and
# all the leaves are at the same level
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
# Returns depth of leftmost leaf
def find_depth(root):
d = 0
while root:
d += 1
root = root.left
return d
def check_perfect(root, d, level=0):
if not root:
return True
# If leaf node, then its depth must be same as that of other nodes
if root.left is None and root.right is None:
return d == (level + 1)
# An internal node with only one child
if root.left is None or root.right is None:
return False
return check_perfect(root.left, d, level+1) and check_perfect(root.right, d, level+1)
def is_perfect(root):
d = find_depth(root)
return check_perfect(root, d)