Java Simple DFS Solution


  • 0
    Z

    Simple DFS solution

    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return helper(root, false);
        }
        
        public int helper(TreeNode root, boolean isLeft) {
            if (root == null) return 0;
            if (root.left == null && root.right == null && isLeft) return root.val;
            int left = helper(root.left, true);
            int right = helper(root.right, false);
            return left + right;
        }
    }
    

Log in to reply
 

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