1302. Deepest Leaves Sum
Previous1269. Number of Ways to Stay in the Same Place After Some StepsNext1310. XOR Queries of a Subarray
Last updated
Was this helpful?
Last updated
Was this helpful?
Given the root
of a binary tree, return the sum of values of its deepest leaves.
Example 1:
Constraints:
The number of nodes in the tree is in the range [1, 10^4]
.
1 <= Node.val <= 100
Tree, Depth-first Search
We can perform the DFS strategy to solve this. During searching, we also maintain a variable h
to keep track of the current height we reach.
h == max height
, we add the current node's value onto the total value;
h > max height
, we update the max height along with the deepest leaf.
Time complexity:
Space complexity: