Java DFS 8ms Easy Solution

  • 0

    Simple preorder traversal with a boolean parameter l indicating whether a left child or not

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

Log in to reply

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