recursive c# solution (easy to understand)


  • 0
    K
    public class Solution {
        
        int total = 0;
        
        public int SumOfLeftLeaves(TreeNode root) {
            PrintTree(root,false);
            return total;
        }
        public void PrintTree(TreeNode root, bool print)
        {
            if (root == null) return;
            if (root.left==null&& root.right==null && print) total+=root.val;
            PrintTree(root.left,true);
            PrintTree(root.right,false);
        }
    }
    

Log in to reply
 

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