列表

详情


1302. 层数最深叶子节点的和

给你一棵二叉树的根节点 root ,请你返回 层数最深的叶子节点的和

 

示例 1:

输入:root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
输出:15

示例 2:

输入:root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
输出:19

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int deepestLeavesSum(TreeNode* root) { } };

golang 解法, 执行用时: 48 ms, 内存消耗: 7.6 MB, 提交时间: 2022-11-15 10:12:20

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func deepestLeavesSum(root *TreeNode) int {
    maxLevel := -1
    sum := 0
    var dfs func(*TreeNode, int)
    dfs = func(node *TreeNode, level int) {
        if node == nil {
            return
        }
        if level > maxLevel {
            maxLevel = level
            sum = node.Val
        } else if level == maxLevel {
            sum += node.Val
        }
        dfs(node.Left, level+1)
        dfs(node.Right, level+1)
    }
    dfs(root, 0)
    return sum
}

golang 解法, 执行用时: 44 ms, 内存消耗: 8.3 MB, 提交时间: 2022-11-15 10:11:39

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func deepestLeavesSum(root *TreeNode) int {
    sum := 0
    q := []*TreeNode{root}
    for len(q) > 0 {
        sum = 0
        size := len(q)
        for i := 0; i < size; i++ {
            node := q[0]
            q = q[1:]
            sum += node.Val
            if node.Left != nil {
                q = append(q, node.Left)
            }
            if node.Right != nil {
                q = append(q, node.Right)
            }
        }
    }
    return sum
}

python3 解法, 执行用时: 184 ms, 内存消耗: 19.2 MB, 提交时间: 2022-11-15 10:10:08

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
        q = deque([root])
        while q:
            ans = 0
            for _ in range(len(q)):
                node = q.popleft()
                ans += node.val
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
        return ans

python3 解法, 执行用时: 188 ms, 内存消耗: 19.3 MB, 提交时间: 2022-11-15 10:09:07

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
        maxLevel, ans = -1, 0
        def dfs(node: Optional[TreeNode], level: int) -> None:
            if node is None: return
            nonlocal maxLevel, ans
            if level > maxLevel:
                maxLevel, ans = level, node.val
            elif level == maxLevel:
                ans += node.val
            dfs(node.left, level + 1)
            dfs(node.right, level + 1)
        dfs(root, 0)
        return ans

上一题