zoukankan      html  css  js  c++  java
  • LeetCode113路径总和 II

    路径总和 II

    题目描述:给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

    叶子节点 是指没有子节点的节点。

    示例说明请见LeetCode官网。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/path-sum-ii/
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解法一:层序遍历

    Queue<Pair<TreeNode, Pair<Integer, List<Integer>>>>这种结构来记录当前节点的路径以及路径和,其中:

    • 外层的Pair的key为当前节点;
    • 内层的Pair的key为当前路径的总和,value为当前路径的记录,从根节点到当前节点。

    然后使用层序遍历的方式使用队列遍历二叉树的节点,当判断某节点的左右节点为空,即为叶子节点,然后判断当前的路径值是否与targetSum相等,如果相等,将相应的路径添加到结果集中。

    最后,返回结果集。

    import com.kaesar.leetcode.TreeNode;
    import javafx.util.Pair;
    
    import java.util.*;
    
    public class LeetCode_113 {
        // 结果集
        private static List<List<Integer>> result = new ArrayList<>();
    
        public static List<List<Integer>> pathSum(TreeNode root, int targetSum) {
            if (root == null) {
                return new ArrayList<>();
            }
            /**
             * 外层的Pair的key为当前节点
             * 内层的Pair的key为当前路径的总和,value为当前路径的记录,从根节点到当前节点
             */
            Queue<Pair<TreeNode, Pair<Integer, List<Integer>>>> nodes = new LinkedList<>();
            List<Integer> path = new ArrayList<>();
            path.add(root.val);
            // 初始化,将根节点添加到队列中
            nodes.add(new Pair<>(root, new Pair<>(root.val, path)));
    
    //        while (!nodes.isEmpty()) {
                Pair<TreeNode, Pair<Integer, List<Integer>>> cur = nodes.poll();
                TreeNode curNode = cur.getKey();
                // 判断当前节点的左右节点为空,即为叶子节点,然后判断当前的路径值是否与targetSum相等
                if (curNode.left == null && curNode.right == null) {
                    if (cur.getValue().getKey() == targetSum) {
                        result.add(cur.getValue().getValue());
                    }
                    continue;
                }
                // 如果当前节点不是叶子节点,继续往下遍历
                if (curNode.left != null) {
                    List<Integer> leftPath = new ArrayList<>(Arrays.asList(new Integer[cur.getValue().getValue().size()]));
                    Collections.copy(leftPath, cur.getValue().getValue());
                    leftPath.add(curNode.left.val);
                    nodes.add(new Pair<>(curNode.left, new Pair<>(cur.getValue().getKey() + curNode.left.val, leftPath)));
                }
                if (curNode.right != null) {
                    List<Integer> rightPath = new ArrayList<>(Arrays.asList(new Integer[cur.getValue().getValue().size()]));
                    Collections.copy(rightPath, cur.getValue().getValue());
                    rightPath.add(curNode.right.val);
                    nodes.add(new Pair<>(curNode.right, new Pair<>(cur.getValue().getKey() + curNode.right.val, rightPath)));
                }
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.right = new TreeNode(3);
            root.right.left = new TreeNode(4);
            root.right.right = new TreeNode(5);
    
            for (List<Integer> integers : pathSum(root, 8)) {
                for (Integer integer : integers) {
                    System.out.print(integer + " ");
                }
                System.out.println();
            }
        }
    }
    

    【每日寄语】 命运,不过是失败者无聊的自慰,不过是懦怯者的解嘲。人们的前途只能靠自己的意志、自己的努力来决定。

  • 相关阅读:
    设备坐标与逻辑坐标
    4个设备上下文DC
    VC6.0智能提示消失恢复
    VC
    JavaWeb_设置Cookie的作用路径
    JavaWeb_Cookie显示最近浏览的商品
    JavaWeb_Cookie
    MVC案例——通过配置切换底层存储源
    MVC案例——修改用户
    MVC案例——删除操作
  • 原文地址:https://www.cnblogs.com/kaesar/p/15576755.html
Copyright © 2011-2022 走看看