From 58e95e1fb039fcb571dfe5321048909fb9e64b6e Mon Sep 17 00:00:00 2001 From: Akansha_chaurasia <125260131+Akansha77@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:32:48 +0000 Subject: [PATCH] Day22 q2: Sum of Left Leaves #464 cpp code added by Akansha77 --- Day-22/q2: Sum of Left Leaves/Akansha--C.cpp | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Day-22/q2: Sum of Left Leaves/Akansha--C.cpp 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