zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Explain
    Beginning Silverlight 4 in C#导航
    ORACLE PL/SQL编程之五: 异常错误处理
    ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素
    ORACLE PL/SQL编程之六: 把过程与函数说透
    ORACLE PL/SQL编程之四: 把游标说透
    ORACLE PL/SQL编程详解之七: 程序包的创建与应用
    ORACLE PL/SQL编程详解之一:PL/SQL 程序设计简介
    ORACLE PL/SQL编程之八: 把触发器说透
    ORACLE PL/SQL编程详解之三: PL/SQL流程控制语句
  • 原文地址:https://www.cnblogs.com/HuangYJ/p/14180981.html
Copyright © 2011-2022 走看看