Sum of Left Leaves @LeetCode
PROBLEM :
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution )
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==NULL)
return 0 ;
int l=0,r=0 ;
if(root->right)
r=sumOfLeftLeaves(root->right) ;
if(root->left&&(root->left->left==NULL&&root->left->right==NULL))
return root->left->val + r ;
if(root->left)
l=sumOfLeftLeaves(root->left) ;
return l+r ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Iterative Solution )
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==NULL)
return 0 ;
queue<TreeNode*> que ;
que.push(root) ;
int sum=0 ;
while(!que.empty()){
int n=que.size() ;
while(n--){
TreeNode* node=que.front() ;
que.pop() ;
if(node->left!=NULL&&(node->left->left==NULL&&node->left->right==NULL))
sum+=node->left->val ;
else if(node->left)
que.push(node->left) ;
if(node->right!=NULL)
que.push(node->right) ;
}
}
return sum ;
}
};
---------------------------------------------------------------------------------
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution )
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==NULL)
return 0 ;
int l=0,r=0 ;
if(root->right)
r=sumOfLeftLeaves(root->right) ;
if(root->left&&(root->left->left==NULL&&root->left->right==NULL))
return root->left->val + r ;
if(root->left)
l=sumOfLeftLeaves(root->left) ;
return l+r ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Iterative Solution )
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==NULL)
return 0 ;
queue<TreeNode*> que ;
que.push(root) ;
int sum=0 ;
while(!que.empty()){
int n=que.size() ;
while(n--){
TreeNode* node=que.front() ;
que.pop() ;
if(node->left!=NULL&&(node->left->left==NULL&&node->left->right==NULL))
sum+=node->left->val ;
else if(node->left)
que.push(node->left) ;
if(node->right!=NULL)
que.push(node->right) ;
}
}
return sum ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment