Simple DFS Solution - Java


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

Log in to reply
 

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