1302. Deepest Leaves Sum
Given a binary tree, return the sum of values of its deepest leaves.
Example 1:
Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15
Constraints:
- The number of nodes in the tree is between
1
and10^4
. - The value of nodes is between
1
and100
.
# @lc code=start
using LeetCode
function deepest_leaves_sum(root::TreeNode)::Int
nodes = [root]
while true
val = 0
for _ in eachindex(nodes)
node = popfirst!(nodes)
val += node.val
isnothing(node.left) || push!(nodes, node.left)
isnothing(node.right) || push!(nodes, node.right)
end
isempty(nodes) && return val
end
end
# @lc code=end
deepest_leaves_sum (generic function with 1 method)
This page was generated using DemoCards.jl and Literate.jl.