美文网首页
404. Sum of Left Leaves

404. Sum of Left Leaves

作者: 安东可 | 来源:发表于2018-04-16 22:46 被阅读16次

404. Sum of Left Leaves
[思路]

  • 寻找左子树的值的和;
  • 遍历

    int sumOfLeftLeaves(TreeNode* root) {
        if(! root)return 0;
        int sum =0;
        sum += sumofleft(root->left,true);
        sum +=sumofleft(root->right,false);
        return sum;

           
    }
    
    int sumofleft(TreeNode* root, bool left){
        if(!root) return 0;
        
        if(! root->left && !root->right && left)
            return root->val;
        
        return sumofleft(root->left,true) + sumofleft(root->right,false);
        
        
    }


相关文章

网友评论

      本文标题:404. Sum of Left Leaves

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