Count Leaf Nodes in a Binary Tree

Given a binary tree, your task is to count and return the number of leaf nodes present in it.

A binary tree is a data structure where each node has at most two children, referred to as the left child and the right child. A leaf node is a node with both left and right child nodes as NULL.

Input:

The first line contains an integer 'T', representing the number of test cases. For each test case, you are given the elements of the binary tree in level order format. Each value of a node is separated by a space on a single line. Use -1 to represent a NULL node.

Output:

For each test case, return the count of leaf nodes in the binary tree.

Example:

Input:
1
20 10 35 5 15 30 42 -1 13 -1 -1 -1 -1 -1 -1 -1
Output:
4
Explanation:
The tree formed by the example input is:
Level 1: 20
Level 2: / \
10 35
Level 3: / \ / \
5 15 30 42
Level 4: / | |
13 (null)
Leaf nodes: 5, 30, 42, 13

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 10^3, where N is the number of nodes in the tree
  • 1 ≤ data ≤ 10^9
  • Time Limit: 1 sec
Note:
Input format is intended to provide clarity on how the binary tree is represented. The sequence will be given in a single line separated by spaces. You only need to implement the function to calculate the leaf nodes, printing will be handled elsewhere.
AnswerBot
4mo

Count and return the number of leaf nodes in a binary tree.

  • Traverse the binary tree and count nodes with both left and right children as NULL.

  • Use recursion to traverse the tree efficiently.

  • Leaf nodes ...read more

Help your peers!
Select
Add answer anonymously...

Ola Cabs Software Developer interview questions & answers

A Software Developer was asked 7mo agoQ. What is a closure?
A Software Developer was asked Q. Minimum Number Of Taps To Water Garden Problem Statement You are required to det...read more
A Software Developer was asked Q. Count Leaf Nodes in a Binary Tree Given a binary tree, your task is to count and...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Minimum Number Of Taps To Water Garden Problem Statement You are required to det...read more
A Software Developer was asked Q2. Count Leaf Nodes in a Binary Tree Given a binary tree, your task is to count and...read more
A Software Developer was asked Q3. Diagonal Traversal of a Binary Tree Problem Statement Given a binary tree, your ...read more
Ola Cabs Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits