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

[pull] master from youngyangyang04:master #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Mar 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
94bdecf
添加474. 一和零 java 三维DP数组实现代码
HelloDam Nov 29, 2024
da742fe
修改0112.路径总和.md的Java版本代码的字母小写问题
curforever Jan 23, 2025
27718a4
docs: 为 0494.目标和.md 完善 JavaDoc 注释, 添加动规五部曲注释, 规范部分代码格式, 修改部分变量名以增强代码语义化
asnsuyu Jan 24, 2025
e6698cb
修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释
curforever Jan 25, 2025
c2e95f6
108冗余连接 java实现
HelloDam Jan 26, 2025
04ad873
Merge branch 'youngyangyang04:master' into master
curforever Feb 3, 2025
333099a
修改二叉树总结篇.md一处列表级别的格式问题
curforever Feb 3, 2025
bb12666
Merge branch 'youngyangyang04:master' into master
asnsuyu Mar 1, 2025
f4addc4
Merge pull request #2874 from curforever/master
youngyangyang04 Mar 5, 2025
bba027e
Merge pull request #2847 from HelloDam/master
youngyangyang04 Mar 5, 2025
e5b75df
Merge pull request #2877 from asnsuyu/master
youngyangyang04 Mar 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释
  • Loading branch information
curforever committed Jan 25, 2025
commit e6698cbac457714427f4a9cba6ea2a3ea9eed94c
46 changes: 28 additions & 18 deletions problems/0530.二叉搜索树的最小绝对差.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,27 @@ public:
递归
```java
class Solution {
TreeNode pre;// 记录上一个遍历的结点
TreeNode pre;// 记录上一个遍历的结点
int result = Integer.MAX_VALUE;

public int getMinimumDifference(TreeNode root) {
if(root==null)return 0;
traversal(root);
return result;
if (root == null)
return 0;
traversal(root);
return result;
}
public void traversal(TreeNode root){
if(root==null)return;
//左

public void traversal(TreeNode root) {
if (root == null)
return;
// 左
traversal(root.left);
//中
if(pre!=null){
result = Math.min(result,root.val-pre.val);
//中
if(pre != null){
result = Math.min(result,root.val - pre.val);
}
pre = root;
//右
//右
traversal(root.right);
}
}
Expand All @@ -182,22 +186,27 @@ class Solution {
TreeNode pre = null;
int result = Integer.MAX_VALUE;

if(root != null)
if(root != null)
stack.add(root);
while(!stack.isEmpty()){

// 中序遍历(左中右),由于栈先入后出,反序(右中左)
while (!stack.isEmpty()) {
TreeNode curr = stack.peek();
if(curr != null){
if(curr != null){
stack.pop();
if(curr.right != null)
// 右
if (curr.right != null)
stack.add(curr.right);
// 中(先用null标记)
stack.add(curr);
stack.add(null);
if(curr.left != null)
// 左
if (curr.left != null)
stack.add(curr.left);
}else{
}else { // 中(遇到null再处理)
stack.pop();
TreeNode temp = stack.pop();
if(pre != null)
if(pre != null)
result = Math.min(result, temp.val - pre.val);
pre = temp;
}
Expand Down Expand Up @@ -674,3 +683,4 @@ public class Solution
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

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