mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释
This commit is contained in:
@ -155,13 +155,17 @@ public:
|
||||
class Solution {
|
||||
TreeNode pre; // 记录上一个遍历的结点
|
||||
int result = Integer.MAX_VALUE;
|
||||
|
||||
public int getMinimumDifference(TreeNode root) {
|
||||
if(root==null)return 0;
|
||||
if (root == null)
|
||||
return 0;
|
||||
traversal(root);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void traversal(TreeNode root) {
|
||||
if(root==null)return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 左
|
||||
traversal(root.left);
|
||||
// 中
|
||||
@ -184,17 +188,22 @@ class Solution {
|
||||
|
||||
if (root != null)
|
||||
stack.add(root);
|
||||
|
||||
// 中序遍历(左中右),由于栈先入后出,反序(右中左)
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode curr = stack.peek();
|
||||
if (curr != null) {
|
||||
stack.pop();
|
||||
// 右
|
||||
if (curr.right != null)
|
||||
stack.add(curr.right);
|
||||
// 中(先用null标记)
|
||||
stack.add(curr);
|
||||
stack.add(null);
|
||||
// 左
|
||||
if (curr.left != null)
|
||||
stack.add(curr.left);
|
||||
}else{
|
||||
} else { // 中(遇到null再处理)
|
||||
stack.pop();
|
||||
TreeNode temp = stack.pop();
|
||||
if (pre != null)
|
||||
@ -674,3 +683,4 @@ public class Solution
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user