Preorder Java beat 85%


  • 0
    F


    public class Solution {
    private int t;
    private List<List<Integer>> res;
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
    res=new ArrayList<>();
    t=sum;
    if(root==null)return res;
    preorder(new ArrayList<Integer>(),0,root);
    return res;
    }
    private void preorder(List<Integer>state, int sum, TreeNode root)
    {
    state.add(root.val);
    sum+=root.val;
    if(root.left==null&&root.right==null &&sum==t)
    res.add(new ArrayList<Integer>(state));
    if(root.left!=null)
    preorder(state,sum,root.left);
    if(root.right!=null)
    preorder(state,sum,root.right);
    state.remove(state.size()-1);
    }
    }


Log in to reply
 

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