Skip to content

Commit 18022dc

Browse files
committedJun 21, 2017
Count Leaf Nodes
1 parent 34069d8 commit 18022dc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎Trees/P02_CountLeafNodes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Author: OMKAR PATHAK
2+
3+
# leaf node is the one which does not have any children
4+
5+
from Tree import Node
6+
7+
def countLeafNodes(root):
8+
if root is None:
9+
return 0
10+
if(root.left is None and root.right is None):
11+
return 1
12+
else:
13+
return countLeafNodes(root.left) + countLeafNodes(root.right)
14+
15+
if __name__ == '__main__':
16+
root = Node(1)
17+
root.setLeft(Node(2))
18+
root.setRight(Node(3))
19+
root.left.setLeft(Node(4))
20+
21+
print('Count of leaf nodes:',countLeafNodes(root))

0 commit comments

Comments
 (0)
Please sign in to comment.