forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountLeafNode.py
More file actions
31 lines (24 loc) · 737 Bytes
/
CountLeafNode.py
File metadata and controls
31 lines (24 loc) · 737 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
# Python program to count leaf nodes in Binary Tree
# A Binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to get the count of leaf nodes in binary tree
def getLeafCount(node):
if node is None:
return 0
if(node.left is None and node.right is None):
return 1
else:
return getLeafCount(node.left) + getLeafCount(node.right)
# Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print "Leaf count of the tree is %d" %(getLeafCount(root))
#This code is contributed by Nikhil Kumar Singh(nickzuck_007)