当前位置:网站首页>#113 Path Sum II
#113 Path Sum II
2022-06-12 20:59:00 【yoyooyooo】
Description
Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.
A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.
Examples
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: []
Example 3:
Input: root = [1,2], targetSum = 0
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 5000].
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
Ideas
What I'm struggling with is what to use as an intermediate storage medium , Later, a List<List<TreeNode>>
But see submission After the solution inside , There is still room for improvement , Try again next time
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
List<List<TreeNode>> all = new ArrayList<>();
List<List<Integer>> answer = new ArrayList<>();
public void addAnswer(int i) {
List<TreeNode> nodes = all.get(i);
List<Integer> sum = new ArrayList<>();
for (TreeNode node: nodes) {
sum.add(node.val);
}
answer.add(sum);
}
public void getSum(List<Integer> sum, int targetSum) {
if (all.size() == 0)
return;
List<List<TreeNode>> next = new ArrayList<>();
List<Integer> nextSum = new ArrayList<>();
for (int i = 0; i < all.size(); i++) {
List<TreeNode> nodes = all.get(i);
TreeNode node = nodes.get(nodes.size() - 1);
int currSum = sum.get(i);
if (node.left == null && node.right == null) {
if (currSum == targetSum) {
addAnswer(i);
}
continue;
}
if (node.left != null) {
List<TreeNode> newTmp = new ArrayList<>(nodes);
newTmp.add(node.left);
next.add(newTmp);
nextSum.add(currSum + node.left.val);
}
if (node.right != null) {
List<TreeNode> newTmp = new ArrayList<>(nodes);
newTmp.add(node.right);
next.add(newTmp);
nextSum.add(currSum + node.right.val);
}
}
all = next;
getSum(nextSum, targetSum);
}
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if (root == null)
return new ArrayList<>();
List<TreeNode> nodes = new ArrayList<>();
nodes.add(root);
all.add(nodes);
List<Integer> sum = new ArrayList<>();
sum.add(root.val);
getSum(sum, targetSum);
return answer;
}
}
边栏推荐
猜你喜欢
![[leetcode 16 solution] the sum of the nearest three numbers](/img/7d/befc79145bb88a2ec39524b2c9bb54.jpg)
[leetcode 16 solution] the sum of the nearest three numbers

竣达技术丨适用于“科士达”智能精密空调网络监控

新品发布丨竣达智能综合环境监测终端

nn. PReLU(planes)

同时做测试,别人已经年薪20w起,为什么你还在为达到月薪10k而努力?

Listener in JSP

金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试

Leetcode: 210. Programme II

New product release Junda intelligent integrated environmental monitoring terminal

Lightroom Ambassador series: capturing nostalgia with MEG loeks
随机推荐
How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
半路自学测试成功转行,第一份测试工作就拿10k
Summary of machine learning materials
What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
Pytoch distributed training error
Cv2.lut() (populates the output array with values from the lookup table)
New product release Junda intelligent integrated environmental monitoring terminal
在同花顺开户安全么,买股票怎么网上开户
MySQL field truncation principle and source code analysis
Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
Deploy etcd cluster in static pod mode
Troubleshooting of service port failure
服务端口不通排查
torch. unique()
leetcode:207. Class Schedule Card
Mxnet record IO details
How to improve communication efficiency during home office | community essay solicitation
最简单ALV模板
Pytorch how to set random number seed to make the result repeatable
Unauthorized rce in VMware vCenter