9 ms Java sol.


  • 0
    A
    public int sumOfLeftLeaves(TreeNode root) {
        if(root ==null || root.left == null && root.right == null){
        
            return 0;
        }
        if(root.left != null){
            visit(root.left, 0);
        }
        if(root.right != null){
            visit(root.right, 1);
        }
        
        return ans;
    }
    
    public void visit(TreeNode r, int c){
        if(r.left != null){
            visit(r.left, 0);
            if(r.right!=null){
                visit(r.right, 1);
            }
        }
        else if(r.right == null ){
            if(c==0){
                ans+=r.val;
            }
        }
        else{
            visit(r.right, 1);
        }
        
    }
    

    }


Log in to reply
 

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