diff --git a/Day-22/q2: Sum of Left Leaves/Akansha--C.cpp b/Day-22/q2: Sum of Left Leaves/Akansha--C.cpp new file mode 100644 index 00000000..19ff8e65 --- /dev/null +++ b/Day-22/q2: Sum of Left Leaves/Akansha--C.cpp @@ -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; + } +}; \ No newline at end of file