LeetCode113. 路径总和 II

本题同剑指24.二叉树中和为某一值的路径

本题相似题目:LeetCode257. 二叉树的所有路径

☆☆☆☆☆方法1:DFS前序遍历 + 回溯

☆☆方法2:BFS层序遍历。

☆☆☆方法3:递归

代码1:DFS前序遍历 + 回溯(耗时1ms)

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        dfs1(root, new ArrayList<>(), res, sum);
//        dfs2(root, new ArrayList<>(), res, 0, sum);
        return res;
    }
    // 回溯 往下减
    private void dfs1(TreeNode root, List<Integer> list, List<List<Integer>> res, int target) {
        if (root == null) return;

        list.add(root.val);

        if (target == root.val && root.left == null && root.right == null) {
            res.add(new ArrayList<>(list));
            list.remove(list.size() - 1); // 注意,此处return前也需要重置
            return;
        }

        dfs1(root.left, list, res, target - root.val);
        dfs1(root.right, list, res, target - root.val);
        list.remove(list.size() - 1); // 递归调用某一节点的左右孩子之后,再移除这个节点。
    }
    // 回溯 往下加
    private void dfs2(TreeNode root, List<Integer> list, List<List<Integer>> res, int temp, int target) {
        if (root == null) return;

        list.add(root.val);
        temp += root.val;

        if (temp == target && root.left == null && root.right == null) {
            res.add(new ArrayList<>(list));
            list.remove(list.size() - 1);
            return;
        }
        dfs2(root.left, list, res, temp, target);
        dfs2(root.right, list, res, temp, target);
        list.remove(list.size() - 1);
    }
}

代码2:BFS层序遍历(耗时4ms)

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        Queue<List<Integer>> pathQueue = new LinkedList<>();
        nodeQueue.offer(root);
        List<Integer> list = new ArrayList<>();
        list.add(root.val);
        pathQueue.offer(list);

        while (!nodeQueue.isEmpty()) {
            TreeNode cur = nodeQueue.poll();
            List<Integer> path = pathQueue.poll();
            if (cur.left == null && cur.right == null) {
                if (cur.val == sum) {
                    res.add(path);
                }
            }
            if (cur.left != null) {
                nodeQueue.offer(cur.left);

                List<Integer> left = new ArrayList<>(path);
                left.add(cur.left.val);
                pathQueue.offer(left);
                cur.left.val += cur.val; // 累加节点的值并修改
            }
            if (cur.right != null) {
                nodeQueue.offer(cur.right);

                List<Integer> right = new ArrayList<>(path);
                right.add(cur.right.val);
                pathQueue.offer(right);
                cur.right.val += cur.val; // 累加节点的值并修改
            }
        }
        return res;
    }
}

代码3:递归(耗时1ms)

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;

        if (root.val == sum && root.left == null && root.right == null) {
            List<Integer> list = new LinkedList<>(); // LinkedList更适合 插入操作
            list.add(root.val);
            res.add(list);
            return res;
        }
        List<List<Integer>> left = pathSum(root.left, sum - root.val);
        for (List<Integer> l : left) {
            l.add(0, root.val);
            res.add(l); // 不要忘了
        }
        List<List<Integer>> right = pathSum(root.right, sum - root.val);
        for (List<Integer> r : right) {
            r.add(0, root.val);
            res.add(r); // 不要忘了
        }
        return res;
    }
}
原文地址:https://www.cnblogs.com/HuangYJ/p/14180981.html