Path Sum II
Title description: Give you the root node root of the binary tree and an integer target and targetSum, and find all the paths whose sum of paths from the root node to the leaf nodes is equal to the given target sum.
leaf node refers to a node without child nodes.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/path-sum-ii/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Sequence traversal
Use
Queue<Pair<TreeNode, Pair<Integer, List<Integer>>>>
to record the path and path sum of the current node, where:
- The key of the outer pair is the current node;
- The key of the inner pair is the sum of the current path, and the value is the record of the current path, from the root node to the current node.
Then use the sequence traversal method to use the queue to traverse the nodes of the binary tree. When judging that the left and right nodes of a node are empty, they are leaf nodes, and then judge whether the current path value is equal to the targetSum, if they are equal, add the corresponding path to the result concentrated.
Finally, the result set is returned.
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();
}
}
}
[Daily Message] Fate is nothing but the boring masturbation of the loser, but the mockery of the cowardly. People's future can only be determined by their own will and their own efforts.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。