美文网首页
1302.deepest-leaves-sum

1302.deepest-leaves-sum

作者: Optimization | 来源:发表于2020-05-21 19:08 被阅读0次
class Solution {
public:
    int deepestLeavesSum(TreeNode* root) {
        int sum = 0;
        int max_depth = 0;
        function<void(TreeNode* ,int)> dfs = [&](TreeNode* n ,int d){
            if(!n) return;
            if(d >max_depth){
                max_depth = d;
                sum = 0;
            }
            if(d == max_depth) sum += n->val;
            dfs(n->left,d+1);
            dfs(n->right,d+1);
        };
        dfs(root,0);
        return sum;
    }
};

网友评论

      本文标题:1302.deepest-leaves-sum

      本文链接:https://www.haomeiwen.com/subject/mvbtahtx.html