Skip to content

Commit

Permalink
Day22 q2: Sum of Left Leaves SnowScriptWinterOfCode#464 cpp code adde…
Browse files Browse the repository at this point in the history
…d by Akansha77
  • Loading branch information
Akansha77 committed Jan 25, 2024
1 parent e7c19ec commit 58e95e1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Day-22/q2: Sum of Left Leaves/Akansha--C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
void solve(TreeNode*root,int &sum,int &flag){
if(root==NULL)return ;
if(root->left==NULL && root->right==NULL &&flag==1){
sum+=root->val;
}
flag=1;
solve(root->left,sum,flag);
flag=0;
solve(root->right,sum,flag);

}
public:
int sumOfLeftLeaves(TreeNode* root) {
int sum=0;
int flag=0;
solve(root,sum,flag);
return sum;
}
};

0 comments on commit 58e95e1

Please sign in to comment.