DFS solution Java


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

Log in to reply
 

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