Easy to understand DFS | C++


  • 1
    S
    class Solution {
    public:
        int sumOfLeftLeaves(TreeNode* root) {
            int sum = 0;
            sumOfLeftLeaves_helper(root, sum, false);
            return sum;
        }
        
        void sumOfLeftLeaves_helper(TreeNode* root, int& sum, bool left){
            if (!root) return;
            if (root->left || root->right){
                sumOfLeftLeaves_helper(root->left, sum, true);
                sumOfLeftLeaves_helper(root->right, sum, false);
            } else if (left){
                sum += root->val;
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.