-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathfull-binary-tree.py
More file actions
35 lines (27 loc) · 881 Bytes
/
full-binary-tree.py
File metadata and controls
35 lines (27 loc) · 881 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
# Python implementation for checking if full binary tree
class Node:
def __init__(self, item):
self.leftC = None
self.rightC = None
self.item = item
def isFullBinaryTree(root):
# Empty Tree
if root is None:
return True
if root.leftC is None and root.rightC is None:
return True
if root.leftC is not None and root.rightC is not None:
return isFullBinaryTree(root.leftC) and isFullBinaryTree(root.rightC)
return False
# Driver Code
root = Node(1)
root.rightChild = Node(3)
root.leftChild = Node(2)
root.leftChild.leftChild = Node(4)
root.leftChild.rightChild = Node(5)
root.leftChild.rightChild.leftChild = Node(6)
root.leftChild.rightChild.rightChild = Node(7)
if(isFullBinaryTree(root)):
print("The tree is a full binary tree")
else:
print("The tree is not a full binary tree")