Accepted Intuitive Java Solution

  • 2
    public static List<List<Integer>> mList = new ArrayList<List<Integer>>();
    public static void helper(TreeNode root, int sum, int val, List<Integer> list) {
        if (root == null) return;
        val += root.val;
        if (sum == val && root.left == null && root.right == null) {
        } else {
            helper(root.left, sum, val, new ArrayList<>(list));
            helper(root.right, sum, val, new ArrayList<>(list));
    public static List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<Integer> l = new ArrayList<>();
        helper(root, sum, 0, l);
        return mList;

Log in to reply

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