int sumOfLeftLeaves(TreeNode* root) {
     if (!root)  return 0;
     int res=0;
     if (root->left && !root->left->left && !root->left->right)
         res += root->left->val;
     else res += sumOfLeftLeaves(root->left);
     return res + sumOfLeftLeaves(root->right);
 }
 int sumOfLeftLeaves(TreeNode* root) {
     if(!root) return 0;
     if(root->left and (!root->left->left and !root->left->right)) {
         return root->left->val
             + sumOfLeftLeaves(root->right);
     }
     return sumOfLeftLeaves(root->left)
         + sumOfLeftLeaves(root->right);
 }
Example #3
0
 int sumOfLeftLeaves(TreeNode* root, bool is_left = false) {
     if (root == NULL) {
         return 0;
     }
     if (root->left == NULL && root->right == NULL && is_left) {
         return root->val;
     }
     return sumOfLeftLeaves(root->left, true) + sumOfLeftLeaves(root->right, false);
 }
 int sumOfLeftLeaves(TreeNode* root) {
     if (root == NULL || isLeave(root)) {
         return 0;
     }
     int sum = sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
     if (isLeave(root->left)) {
         sum += root->left->val;
     }
     return sum;
 }
Example #5
0
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int sumOfLeftLeaves(struct TreeNode* root) {
    if (!root || (!root->left && !root->right))
        return 0;
    int rightsum = sumOfLeftLeaves(root->right);
    struct TreeNode* left = root->left;
    if (left && !left->left && !left->right)
        return root->left->val + rightsum;
    else
        return sumOfLeftLeaves(root->left) + rightsum;
    
}
Example #6
0
 int sumOfLeftLeaves(TreeNode* root) {
     if (!root) {
         return 0;
     }
     
     if (root->left && !root->left->left && !root->left->right) {
         return root->left->val + sumOfLeftLeaves(root->right);
     }
     
     return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
 }
Example #7
0
 int sumOfLeftLeaves(TreeNode* root) {  
     if (root == nullptr) return 0;  
     if (root->left) {  
         if (root->left->left == nullptr && root->left->right == nullptr) {  
             return root->left->val + sumOfLeftLeaves(root->right);  
         } else {  
             return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);  
         }  
     } else {  
         return sumOfLeftLeaves(root->right);  
     }  
 }  
 int sumOfLeftLeaves(TreeNode* root) {
     if (!root) return 0;
     int ans = 0;
     if (root->left) { // check if we could finish search on left child and add it to sum
         if (root->left->left == nullptr && root->left->right == nullptr) {
             ans += root->left->val;
         } else {
             ans += sumOfLeftLeaves(root->left);
         }
     }
     ans += sumOfLeftLeaves(root->right);
     return ans;
 }
 int sumOfLeftLeaves(TreeNode* root, bool isLeft = false) {
     if (!root)
     {
         return 0;
     }
     
     if ((root->left == nullptr) && (root->right == nullptr))
     {
         return isLeft ? root->val : 0;
     }
     
     return sumOfLeftLeaves(root->left, true) + sumOfLeftLeaves(root->right, false);
 }
 int sumOfLeftLeaves(TreeNode* root) {
     if(root==NULL)
         return 0;
     int left = 0; //to calculate left sum
     if(root->left &&root->left->left == NULL && root->left->right == NULL) //check for leaf left node
     {
         left = root->left->val;
     }
     else
     {
         left = sumOfLeftLeaves(root->left); //recur in left subtree
     }
     //compulsory
     int right = sumOfLeftLeaves(root->right); //recur in right subtree for left leaf nodes
     return left+right;
 }
 int sumOfLeftLeaves(TreeNode* root) {
     if(root==NULL) return 0;
     if(root->left && root->left->left==NULL && root->left->right==NULL) return root->left->val + sumOfLeftLeaves(root->right);
     return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
 }