0% completed
Problem Statement
Given the root
of a binary tree, return the sum of all left leaves.
A leaf is a node that does not have any child nodes, and a left leaf is a leaf that is the left child of its parent.
Examples
Example 1:
- Input: root =
[3,5,10,null,null,8,7]
- Expected Output:
13
- Justification: The leaf nodes are
5
,8
, and7
, but only5
and8
are left leaf nodes. So, sum of left leaf nodes are 5 + 8 = 13.
Example 2:
- Input: root =
[5, 3, 8, 2, 4, null, 6, 1]
- Expected Output:
1
- Justification: The only left leaf node is
1
(left child of2
). So, the answer is 1.
Example 3:
- Input: root =
[1, null, 5, null, 4]
- Expected Output:
0
- Justification: The tree doesn't have any left leaf node.
Constraints:
- The number of nodes in the tree is in the range [1, 1000].
- -1000 <= Node.val <= 1000
Solution
To solve this problem, we use Depth-First Search (DFS) to traverse the binary tree and find all left leaf nodes. Starting from the root, we recursively explore each node's left and right children. If a node is a leaf (it has no children) and is a left child (tracked using a boolean flag), we add its value to the total sum. For every node, we check its left child by setting the flag as true
and its right child with the flag as false
. This approach ensures that only left leaf nodes are considered in the sum, and we continue this process until all nodes are visited.
Step-by-Step Algorithm
Start from the root node:
- Call
dfs
with the root node andisLeft
set tofalse
to indicate it is not a left child.
DFS() Function
-
Check for a null node:
- If the current
node
isnull
, return0
. This means there are no further nodes in this path.
- If the current
-
Identify a left leaf node:
- If the current
node
is a leaf (node.left == null && node.right == null
) andisLeft
istrue
, returnnode.val
. This adds the value of the left leaf node to the sum.
- If the current
-
Recursive traversal for children:
- Call
dfs
recursively for the left child (node.left
) withisLeft
set totrue
to track it as a left child. - Call
dfs
recursively for the right child (node.right
) withisLeft
set tofalse
.
- Call
-
Calculate total sum:
- Return the sum of the results from the left and right child calls to accumulate the total sum of all left leaves in the tree.
Algorithm Walkthrough
Input: root = [3, 5, 10, null, null, 8, 7]
:
Start with the root node:
- The root node is
3
. Calldfs(3, false)
. node
is notnull
, so proceed further.
DFS() Function
-
Check if root node is a left leaf:
node
3
is not a leaf (it has left and right children). Move to the next steps.
-
Recursive call for the left child of root (3):
- Call
dfs(5, true)
. node
5
is notnull
, so proceed further.node
5
is a leaf node (node.left == null && node.right == null
) andisLeft
istrue
.- Return
5
(its value) as it is a left leaf.
- Call
-
Recursive call for the right child of root (3):
- Call
dfs(10, false)
. node
10
is notnull
, so proceed further.node
10
is not a leaf node (it has left and right children), so move to the next steps.
- Call
-
Recursive call for the left child of node (10):
- Call
dfs(8, true)
. node
8
is notnull
, so proceed further.node
8
is a leaf node (node.left == null && node.right == null
) andisLeft
istrue
.- Return
8
(its value) as it is a left leaf.
- Call
-
Recursive call for the right child of node (10):
- Call
dfs(7, false)
. node
7
is notnull
, so proceed further.node
7
is a leaf node, butisLeft
isfalse
.- Return
0
as it is not a left leaf.
- Call
-
Combine results for node (10):
- The result for
node
10
isdfs(8, true) + dfs(7, false)
, which is8 + 0 = 8
. - Return
8
.
- The result for
-
Combine results for root node (3):
- The result for the root node
3
isdfs(5, true) + dfs(10, false)
, which is5 + 8 = 13
. - Return
13
.
- The result for the root node
-
Final result:
- The total sum of left leaves in the tree
[3,5,10,null,null,8,7]
is13
.
- The total sum of left leaves in the tree
Code
Complexity Analysis
Time Complexity
- The time complexity of the above solution is O(n), where
n
is the number of nodes in the binary tree. - This is because the algorithm visits each node exactly once in a Depth-First Search (DFS) manner to determine whether it is a left leaf.
Space Complexity
- The space complexity of the solution is O(h), where
h
is the height of the binary tree. - This is due to the recursion stack used in the DFS. In the worst case, if the tree is completely unbalanced, the recursion stack would go up to O(n), but in a balanced binary tree, it would be O(log n).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible