easy understand java recursive solution


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

Log in to reply
 

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