My simple java solution

  • 0
    public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> allpath = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        findpath(root, path, allpath, sum);
        return allpath;
    public void findpath(TreeNode cur, List<Integer> path, List<List<Integer>> allpath, int sum){
        List<Integer> temp = new ArrayList<>(path);
        if(cur != null){
            if(cur.val == sum && cur.left == null && cur.right == null) allpath.add(temp);
                findpath(cur.left, temp, allpath, sum - cur.val);
                findpath(cur.right, temp, allpath, sum - cur.val);


Log in to reply

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