mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
update 0112.路径总和: 优化 java 和 js 部分代码格式
This commit is contained in:
@ -33,7 +33,7 @@
|
|||||||
* 112.路径总和
|
* 112.路径总和
|
||||||
* 113.路径总和ii
|
* 113.路径总和ii
|
||||||
|
|
||||||
这道题我们要遍历从根节点到叶子节点的的路径看看总和是不是目标和。
|
这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。
|
||||||
|
|
||||||
### 递归
|
### 递归
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
**是不是发现精简之后的代码,已经完全看不出分析的过程了,所以我们要把题目分析清楚之后,在追求代码精简。** 这一点我已经强调很多次了!
|
**是不是发现精简之后的代码,已经完全看不出分析的过程了,所以我们要把题目分析清楚之后,再追求代码精简。** 这一点我已经强调很多次了!
|
||||||
|
|
||||||
|
|
||||||
### 迭代
|
### 迭代
|
||||||
@ -316,13 +316,13 @@ class solution {
|
|||||||
}
|
}
|
||||||
if (root.left != null) {
|
if (root.left != null) {
|
||||||
boolean left = haspathsum(root.left, targetsum);
|
boolean left = haspathsum(root.left, targetsum);
|
||||||
if (left) {// 已经找到
|
if (left) { // 已经找到
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (root.right != null) {
|
if (root.right != null) {
|
||||||
boolean right = haspathsum(root.right, targetsum);
|
boolean right = haspathsum(root.right, targetsum);
|
||||||
if (right) {// 已经找到
|
if (right) { // 已经找到
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -348,31 +348,37 @@ class solution {
|
|||||||
```java
|
```java
|
||||||
class solution {
|
class solution {
|
||||||
public boolean haspathsum(treenode root, int targetsum) {
|
public boolean haspathsum(treenode root, int targetsum) {
|
||||||
if(root==null)return false;
|
if(root == null) return false;
|
||||||
stack<treenode> stack1 = new stack<>();
|
stack<treenode> stack1 = new stack<>();
|
||||||
stack<integer> stack2 = new stack<>();
|
stack<integer> stack2 = new stack<>();
|
||||||
stack1.push(root);stack2.push(root.val);
|
stack1.push(root);
|
||||||
while(!stack1.isempty()){
|
stack2.push(root.val);
|
||||||
|
while(!stack1.isempty()) {
|
||||||
int size = stack1.size();
|
int size = stack1.size();
|
||||||
for(int i=0;i<size;i++){
|
|
||||||
treenode node = stack1.pop();int sum=stack2.pop();
|
for(int i = 0; i < size; i++) {
|
||||||
|
treenode node = stack1.pop();
|
||||||
|
int sum = stack2.pop();
|
||||||
|
|
||||||
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
|
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
|
||||||
if(node.left==null && node.right==null && sum==targetsum)return true;
|
if(node.left == null && node.right == null && sum == targetsum) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
|
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
|
||||||
if(node.right!=null){
|
if(node.right != null){
|
||||||
stack1.push(node.right);stack2.push(sum+node.right.val);
|
stack1.push(node.right);
|
||||||
|
stack2.push(sum + node.right.val);
|
||||||
}
|
}
|
||||||
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
|
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
|
||||||
if(node.left!=null){
|
if(node.left != null) {
|
||||||
stack1.push(node.left);stack2.push(sum+node.left.val);
|
stack1.push(node.left);
|
||||||
|
stack2.push(sum + node.left.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 0113.路径总和-ii
|
### 0113.路径总和-ii
|
||||||
@ -715,21 +721,21 @@ var pathsum = function(root, targetsum) {
|
|||||||
//递归方法
|
//递归方法
|
||||||
let respath = [],curpath = [];
|
let respath = [],curpath = [];
|
||||||
// 1. 确定递归函数参数
|
// 1. 确定递归函数参数
|
||||||
const traveltree = function(node,count){
|
const traveltree = function(node,count) {
|
||||||
curpath.push(node.val);
|
curpath.push(node.val);
|
||||||
count-=node.val;
|
count -= node.val;
|
||||||
if(node.left===null&&node.right===null&&count===0){
|
if(node.left === null && node.right === null && count === 0) {
|
||||||
respath.push([...curpath]);
|
respath.push([...curpath]);
|
||||||
}
|
}
|
||||||
node.left&&traveltree(node.left,count);
|
node.left && traveltree(node.left, count);
|
||||||
node.right&&traveltree(node.right,count);
|
node.right && traveltree(node.right, count);
|
||||||
let cur = curpath.pop();
|
let cur = curpath.pop();
|
||||||
count-=cur;
|
count -= cur;
|
||||||
}
|
}
|
||||||
if(root===null){
|
if(root === null) {
|
||||||
return respath;
|
return respath;
|
||||||
}
|
}
|
||||||
travelTree(root,targetSum);
|
travelTree(root, targetSum);
|
||||||
return resPath;
|
return resPath;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user