404. Sum of Left Leaves

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)