Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4addc4

Browse files
Merge pull request youngyangyang04#2874 from curforever/master
修改0112.路径总和.md的Java版本代码的字母小写问题、修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释、修改二叉树总结篇.md一处列表级别的格式问题
2 parents 5bd753f + 333099a commit f4addc4

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

‎problems/0112.路径总和.md

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,25 @@ public:
309309
0112.路径总和
310310

311311
```java
312-
class solution {
313-
public boolean haspathsum(treenode root, int targetsum) {
312+
class Solution {
313+
public boolean hasPathSum(TreeNode root, int targetSum) {
314314
if (root == null) {
315315
return false;
316316
}
317-
targetsum -= root.val;
317+
targetSum -= root.val;
318318
// 叶子结点
319319
if (root.left == null && root.right == null) {
320-
return targetsum == 0;
320+
return targetSum == 0;
321321
}
322322
if (root.left != null) {
323-
boolean left = haspathsum(root.left, targetsum);
324-
if (left) { // 已经找到
323+
boolean left = hasPathSum(root.left, targetSum);
324+
if (left) { // 已经找到,提前返回
325325
return true;
326326
}
327327
}
328328
if (root.right != null) {
329-
boolean right = haspathsum(root.right, targetsum);
330-
if (right) { // 已经找到
329+
boolean right = hasPathSum(root.right, targetSum);
330+
if (right) { // 已经找到,提前返回
331331
return true;
332332
}
333333
}
@@ -336,39 +336,39 @@ class solution {
336336
}
337337

338338
// lc112 简洁方法
339-
class solution {
340-
public boolean haspathsum(treenode root, int targetsum) {
339+
class Solution {
340+
public boolean hasPathSum(TreeNode root, int targetSum) {
341341

342342
if (root == null) return false; // 为空退出
343343

344344
// 叶子节点判断是否符合
345-
if (root.left == null && root.right == null) return root.val == targetsum;
345+
if (root.left == null && root.right == null) return root.val == targetSum;
346346

347347
// 求两侧分支的路径和
348-
return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val);
348+
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
349349
}
350350
}
351351
```
352352

353353
迭代
354354

355355
```java
356-
class solution {
357-
public boolean haspathsum(treenode root, int targetsum) {
356+
class Solution {
357+
public boolean hasPathSum(TreeNode root, int targetSum) {
358358
if(root == null) return false;
359-
stack<treenode> stack1 = new stack<>();
360-
stack<integer> stack2 = new stack<>();
359+
Stack<TreeNode> stack1 = new Stack<>();
360+
Stack<Integer> stack2 = new Stack<>();
361361
stack1.push(root);
362362
stack2.push(root.val);
363-
while(!stack1.isempty()) {
363+
while(!stack1.isEmpty()) {
364364
int size = stack1.size();
365365

366366
for(int i = 0; i < size; i++) {
367-
treenode node = stack1.pop();
367+
TreeNode node = stack1.pop();
368368
int sum = stack2.pop();
369369

370370
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
371-
if(node.left == null && node.right == null && sum == targetsum) {
371+
if(node.left == null && node.right == null && sum == targetSum) {
372372
return true;
373373
}
374374
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
@@ -387,8 +387,9 @@ class solution {
387387
}
388388
}
389389
```
390-
```Java 統一迭代法
391-
public boolean hasPathSum(TreeNode root, int targetSum) {
390+
```Java
391+
class Solution {
392+
public boolean hasPathSum(TreeNode root, int targetSum) {
392393
Stack<TreeNode> treeNodeStack = new Stack<>();
393394
Stack<Integer> sumStack = new Stack<>();
394395

@@ -422,38 +423,39 @@ class solution {
422423
}
423424
return false;
424425
}
426+
}
425427
```
426428

427429
0113.路径总和-ii
428430

429431
```java
430-
class solution {
431-
public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
432+
class Solution {
433+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
432434
List<List<Integer>> res = new ArrayList<>();
433435
if (root == null) return res; // 非空判断
434436

435437
List<Integer> path = new LinkedList<>();
436-
preorderdfs(root, targetsum, res, path);
438+
preOrderDfs(root, targetSum, res, path);
437439
return res;
438440
}
439441

440-
public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
442+
public void preOrderDfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
441443
path.add(root.val);
442444
// 遇到了叶子节点
443445
if (root.left == null && root.right == null) {
444446
// 找到了和为 targetsum 的路径
445-
if (targetsum - root.val == 0) {
447+
if (targetSum - root.val == 0) {
446448
res.add(new ArrayList<>(path));
447449
}
448450
return; // 如果和不为 targetsum,返回
449451
}
450452

451453
if (root.left != null) {
452-
preorderdfs(root.left, targetsum - root.val, res, path);
454+
preOrderDfs(root.left, targetSum - root.val, res, path);
453455
path.remove(path.size() - 1); // 回溯
454456
}
455457
if (root.right != null) {
456-
preorderdfs(root.right, targetsum - root.val, res, path);
458+
preOrderDfs(root.right, targetSum - root.val, res, path);
457459
path.remove(path.size() - 1); // 回溯
458460
}
459461
}
@@ -1626,3 +1628,4 @@ public class Solution {
16261628
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
16271629
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
16281630
</a>
1631+

‎problems/0530.二叉搜索树的最小绝对差.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,27 @@ public:
153153
递归
154154
```java
155155
class Solution {
156-
TreeNode pre;// 记录上一个遍历的结点
156+
TreeNode pre;// 记录上一个遍历的结点
157157
int result = Integer.MAX_VALUE;
158+
158159
public int getMinimumDifference(TreeNode root) {
159-
if(root==null)return 0;
160-
traversal(root);
161-
return result;
160+
if (root == null)
161+
return 0;
162+
traversal(root);
163+
return result;
162164
}
163-
public void traversal(TreeNode root){
164-
if(root==null)return;
165-
//左
165+
166+
public void traversal(TreeNode root) {
167+
if (root == null)
168+
return;
169+
// 左
166170
traversal(root.left);
167-
//中
168-
if(pre!=null){
169-
result = Math.min(result,root.val-pre.val);
171+
//中
172+
if(pre != null){
173+
result = Math.min(result,root.val - pre.val);
170174
}
171175
pre = root;
172-
//右
176+
//右
173177
traversal(root.right);
174178
}
175179
}
@@ -182,22 +186,27 @@ class Solution {
182186
TreeNode pre = null;
183187
int result = Integer.MAX_VALUE;
184188

185-
if(root != null)
189+
if(root != null)
186190
stack.add(root);
187-
while(!stack.isEmpty()){
191+
192+
// 中序遍历(左中右),由于栈先入后出,反序(右中左)
193+
while (!stack.isEmpty()) {
188194
TreeNode curr = stack.peek();
189-
if(curr != null){
195+
if(curr != null){
190196
stack.pop();
191-
if(curr.right != null)
197+
//
198+
if (curr.right != null)
192199
stack.add(curr.right);
200+
// 中(先用null标记)
193201
stack.add(curr);
194202
stack.add(null);
195-
if(curr.left != null)
203+
//
204+
if (curr.left != null)
196205
stack.add(curr.left);
197-
}else{
206+
}else { // 中(遇到null再处理)
198207
stack.pop();
199208
TreeNode temp = stack.pop();
200-
if(pre != null)
209+
if(pre != null)
201210
result = Math.min(result, temp.val - pre.val);
202211
pre = temp;
203212
}
@@ -674,3 +683,4 @@ public class Solution
674683
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
675684
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
676685
</a>
686+

‎problems/二叉树总结篇.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@
9292
* 递归:中序,双指针操作
9393
* 迭代:模拟中序,逻辑相同
9494
* [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html)
95-
95+
9696
* 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合
97-
* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html)
98-
97+
* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html)
9998
* 递归:中序,双指针操作累加
10099
* 迭代:模拟中序,逻辑相同
101100

@@ -163,3 +162,4 @@
163162
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
164163
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
165164
</a>
165+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /