1302.deepest-leaves-sum

2020-05-21  本文已影响0人  Optimization
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;
    }
};
上一篇下一篇

猜你喜欢

热点阅读