关于java:LeetCode113路径总和-II

门路总和 II

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

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

示例阐明请见LeetCode官网。

起源:力扣(LeetCode)
链接:https://leetcode-cn.com/probl…
著作权归领扣网络所有。商业转载请分割官网受权,非商业转载请注明出处。

解法一:层序遍历

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();
        }
    }
}

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理