当前位置:网站首页>#113 Path Sum II
#113 Path Sum II
2022-06-12 20:58: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
思路
我纠结的点在于用什么作为中间存储介质,后面建立了一个List<List<TreeNode>>
但是看到submission里面的解法之后,确实是还有改进的空间,下次重新试试
代码
/** * 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;
}
}
边栏推荐
- [tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
- 函数的了解
- Integrated monitoring solution for power environment of small and medium-sized computer rooms
- Research Report on market supply and demand and strategy of China's hydraulic hammer industry
- Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
- Can flush open an account? Can you directly open the security of securities companies on the app? How to open an account online when buying stocks
- Access control system based on RFID
- Let Google browser fofa plug-in come alive
- Maximize tensorflow* CPU performance (shell)
- JSON file handles object Tags
猜你喜欢

leetcode:207. Class Schedule Card

Listener in JSP

New product release Junda intelligent integrated environmental monitoring terminal

Data visualization - biaxial comparison effect

2022年春招,测试工程师全套面试攻略,一篇吃透全部技术栈(全是干货)

(11) Image frequency domain filtering with OpenCV

What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8

解决cvxpy报错The solver GLPK_MI is not installed

EditText control starts from the upper left corner

Event distribution mechanism of view
随机推荐
go --- 监控文件变化
Properties to YML
Technology to understand
What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
Social metauniverse: start from redefining yourself
作用域和作用域链
remote: Support for password authentication was removed on August 13, 2021
Summary of machine learning materials
CUDA out of memory
Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot
leetcode:207. 课程表
Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?
解决cvxpy报错The solver GLPK_MI is not installed
JS中如何实现重载
最简单ALV模板
Unauthorized rce in VMware vCenter
DFT learning notes
Data visualization - Calendar chart
半路自学测试成功转行,第一份测试工作就拿10k