0% completed
Problem Statement
Given a root
of the binary tree, return true
if all the leaf nodes of this tree exist at the same depth. Otherwise, return false
.
Examples
Example 1:
- Input: root =
[1, 2, 3, 4, null, null, 5]
- Expected Output:
true
- Justification: The leaves are nodes
4
and5
, and they are both at the same level (level 3).
Example 2:
- Input: root =
[12, 20, 13, null, 10, 11, null, 16]
- Expected Output:
false
- Justification: The leaf node
11
is at depth3
and leaf node16
is at depth4
. So, both leaf nodes are at different depth.
Example 3:
- Input: root =
[10, 8, 15, 17, null, 12]
- Expected Output:
true
- Justification: The leaves are nodes
17
, and12
, and all are located at the same level (level 3).
Solution
To solve this problem, we can use a depth-first search (DFS) traversal of the tree. DFS will allow us to explore the tree deeply, moving to the leaf nodes efficiently. During this traversal, we can keep track of the depth at which we encounter the first leaf node. As we visit each additional leaf, we compare its depth with that of the first leaf. If any leaf is found at a different depth, we know the leaves are not at the same level, and we can return false.
The advantage of DFS is that it traverses each path fully, allowing us to immediately detect depth differences without the need for additional tree traversals. This approach minimizes both time and space complexity by only requiring a single pass through the tree.
Step-by-step Algorithm
-
Initialize referenceDepth:
- Set
referenceDepth
to -1. This variable will store the depth of the first leaf node we find.
- Set
-
Start DFS Traversal:
- Begin the DFS traversal from the root of the tree, starting with depth 0.
-
Handle empty trees:
- If the current node is
null
, returntrue
. This means there’s no node to process, and we can ignore this subtree.
- If the current node is
-
Check if the node is a leaf:
- If the current node has no left or right children, it's a leaf node. For the first leaf node found, store its depth in
referenceDepth
. - If this is not the first leaf node, compare its depth to
referenceDepth
. If the depths matches, returntrue
. Otherwise, returnfalse
.
- If the current node has no left or right children, it's a leaf node. For the first leaf node found, store its depth in
-
Continue DFS on children:
- If the node is not a leaf, continue DFS on its left and right children, increasing the depth by 1 each time.
-
Return the result:
- Combine the returned value from the DFS traversal in the left and right subtree using the '&&' (and) operation, and return the final resultant boolean value.
Algorithm Walkthrough
Input: root = [1, 2, 3, 4, null, null, 5]
The binary tree looks like this:
1
/ \
2 3
/ \
4 5
We'll walk through the algorithm step by step:
-
Start DFS from root (node 1) at depth 0.
- Node 1 is not a leaf, so we move to its left and right children.
-
DFS on left child of node 1 (node 2) at depth 1.
- Node 2 is not a leaf, so we move to its left child.
-
DFS on left child of node 2 (node 4) at depth 2.
- Node 4 is a leaf. Since this is the first leaf encountered, set
referenceDepth = 2
.
- Node 4 is a leaf. Since this is the first leaf encountered, set
-
Backtrack to node 1 and DFS on right child of node 1 (node 3) at depth 1.
- Node 3 is not a leaf, so we move to its right child as left child is null.
-
DFS on right child of node 3 (node 5) at depth 2.
- Node 5 is a leaf. Compare its depth (2) with
referenceDepth
(2). Since they match, we continue.
- Node 5 is a leaf. Compare its depth (2) with
-
All leaf nodes are at the same depth.
- Both leaves (4 and 5) are at depth 2. Since no mismatch is found.
Final Output:
- The function returns
true
, meaning all leaf nodes are at the same level in this tree.
Code
Complexity Analysis
Time Complexity
The time complexity of the algorithm is O(n), where n
is the number of nodes in the binary tree. This is because the DFS traversal visits every node exactly once.
Space Complexity
The space complexity is O(h), where h
is the height of the binary tree. This comes from the recursion stack used by the DFS traversal. In the worst case (a skewed tree), the recursion depth can be equal to the total number of nodes in the tree and the overall space complexity can be O(n).
.....
.....
.....
On this page
Problem Statement
Examples
Solution
Step-by-step Algorithm
Algorithm Walkthrough
Final Output:
Code
Complexity Analysis
Time Complexity
Space Complexity