C# recursive solution easy to understand


  • 0
    Y
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int SumOfLeftLeaves(TreeNode root) {
            int res = 0;
            if(root == null)
                return res;
            if(root.left != null && root.left.left == null && root.left.right == null)
                res += root.left.val;
            else
                res += SumOfLeftLeaves(root.left);
            res += SumOfLeftLeaves(root.right);
            return res;
        }
    }

Log in to reply
 

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