c# solution using recursion


  • 0
    M
     public class Solution {
       int result = 0;
    
            public int SumOfLeftLeaves(TreeNode root)
            {
                SumOfLeftLeavesRec(root);
    
                return result;
            }
    
            private void SumOfLeftLeavesRec(TreeNode node, bool isLeft = false)
            {
                if (node == null) return;
                if (isLeft && node.left == null && node.right == null) result += node.val;
    
                SumOfLeftLeavesRec(node.left, true);
                SumOfLeftLeavesRec(node.right);
            }
    }
    

Log in to reply
 

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