
About the author
Mallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗ProgramGuru
| Input Tree | All Leaves Same Level? | Description |
|---|---|---|
| [1, 2, 3, 4, 5, null, null] | false | Leaf 3 is at level 2; leaves 4 and 5 are at level 3 — not the same. |
| [1, 2, 3, null, 4] | false | Leaf 3 is at level 2; leaf 4 is at level 3 — different levels. |
| [7] | true | Only one node, which is also a leaf — trivially same level. |
| [] | true | No nodes means no leaf nodes — considered valid by default. |
| [1, 2, null, 3, null, 4] | true | Only one leaf (node 4) exists — all leaves are at the same level. |
| [10, 20, 30, 40, 50, 60, 70] | true | All leaf nodes (40, 50, 60, 70) are at the same level (level 3). |
We are given a binary tree and our goal is to determine whether all the leaf nodes (nodes with no children) are located at the same level (i.e., same depth from the root).
For example, in the tree below:
1
/ 2 3
/ 4 5
The leaves are nodes 4 and 5, and both are at level 2 (root is at level 0). Since all leaves are at the same level, the answer is true.
However, if the tree was:
1
/ 2 3
/
4
/
6
Then, node 3 is a leaf at level 1, while node 6 is a leaf at level 3 — the leaf nodes are not at the same level, so the answer is false.
To compare the levels of all leaf nodes, we can perform a level order traversal (Breadth-First Search), or a depth-first traversal with depth tracking. We'll use DFS here, keeping track of the level of each leaf node.
We define a recursive function that receives the current node and its level. When it reaches a leaf node, it checks: - Is this the first leaf? Record its level. - Is this a later leaf? Check if its level matches the first leaf’s level.
Let's take the following binary tree:
10
/ 5 20
/ 15 25
Leaves: 5, 15, 25 — all are at level 2. So the output is true.
Now, if node 5 had a left child:
10
/ 5 20
/ / 2 15 25
Leaf nodes: 2 (level 2), 15 (level 2), 25 (level 2) → still true.
But if node 2 had a child:
10
/ 5 20
/ / 2 15 25
/
1
Now leaves: 1 (level 3), 15 (level 2), 25 (level 2) → not at same level → false.
We return true only if all leaf nodes encountered match the level of the first leaf node.
If the tree is empty (i.e., root is null), then there are no leaf nodes. Since there's nothing to violate the rule, we return true.
If the tree contains only the root node, then it's a leaf by itself. Since it’s the only leaf, the condition is trivially satisfied. Output: true.
Even if the tree structure is unbalanced, we return true if the leaf nodes all occur at the same level.
If we find any leaf at a different level than the others, we immediately return false.
This problem tests your understanding of tree traversal and level management. The key idea is to track the depth of all leaf nodes and ensure they are the same. It’s a common technique in problems related to balance and symmetry in binary trees. Edge cases like empty trees and single-node trees must be handled first to ensure correctness.
true.queue and enqueue the root node along with its level (0).leafLevel to -1 to record the level of the first encountered leaf node.leafLevel is -1, set it to the current level.leafLevel, return false.true as all leaves are at the same level.#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
TreeNode* createNode(int val) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = node->right = NULL;
return node;
}
int main() {
TreeNode* root = createNode(10);
printf("Root value is: %d\n", root->val);
free(root);
return 0;
}
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗