Java Recursive solution


  • 0
    T
        int sum = 0;
        public int sumOfLeftLeaves(TreeNode root) {
            goToLeft(root,0);
            return sum;
        }
        
        public void goToLeft(TreeNode root,int direction) {
            if(root==null)
               return;
               
            if(root.left==null && root.right==null && direction==1) {
               sum+=root.val;
               return;
            }
            goToLeft(root.left,1);
            goToLeft(root.right,0);
        }
    }

Log in to reply
 

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