We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 34069d8 commit 18022dcCopy full SHA for 18022dc
Trees/P02_CountLeafNodes.py
@@ -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