A node is a leaf node if both left and right child nodes of it are NULL.
Here is an algorithm to get the leaf node count.
getLeafCount(node) 1) If node is NULL then return 0. 2) Else If left and right child nodes are NULL return 1. 3) Else recursively calculate leaf count of the tree using below formula. Leaf count of a tree = Leaf count of left subtree + Leaf count of right subtree
Leaf count for the above tree is 3.
The leaf count of binary tree is : 3
Time & Space Complexities: Since this program is similar to traversal of tree, time and space complexities will be same as Tree traversal (Please see our Tree Traversal post for details)
Please write comments if you find any bug in the above programs/algorithms or other ways to solve the same problem.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Iterative program to count leaf nodes in a Binary Tree
- Count Non-Leaf nodes in a Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all leaf nodes of binary tree
- Product of all leaf nodes of binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print all leaf nodes of a binary tree from right to left
- Print the nodes of binary tree as they become the leaf node
- Pairwise Swap leaf nodes in a binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Determine the count of Leaf nodes in an N-ary tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Print left and right leaf nodes separately in Binary Tree
- Print leaf nodes in binary tree from left to right using one stack
- Find height of a special binary tree whose leaf nodes are connected
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )