-
Notifications
You must be signed in to change notification settings - Fork 134
/
Check_Full_BinaryTree.py
48 lines (37 loc) · 1.22 KB
/
Check_Full_BinaryTree.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
46
47
48
# Check whether a binary tree is a full binary tree or not
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def check_full_bt(root):
# If tree is empty
if root is None:
return True
# if both child are None
if root.left is None and root.right is None:
return True
# if the node has both children and both sub tree are full binary trees
if root.left is not None and root.right is not None:
return check_full_bt(root.left) and check_full_bt(root.right)
return False
if __name__ == '__main__':
root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.left.right = Node(40)
root.left.left = Node(50)
root.right.left = Node(60)
root.right.right = Node(70)
root.left.left.left = Node(80)
root.left.left.right = Node(90)
root.left.right.left = Node(80)
root.left.right.right = Node(90)
root.right.left.left = Node(80)
root.right.left.right = Node(90)
root.right.right.left = Node(80)
root.right.right.right = Node(90)
if check_full_bt(root):
print('Yes, given binary tree is Full BT')
else:
print('No, given binary tree is not Full BT')