Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion problems/0035.搜索插入位置.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func searchInsert(nums []int, target int) int {
left = mid + 1
}
}
return len(nums)
return right+1
}
```

Expand Down
30 changes: 30 additions & 0 deletions problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,36 @@ public:

## Java

```Java
//使用統一迭代法
class Solution {
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
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);
stack.add(curr);
stack.add(null);
if(curr.left != null)
stack.add(curr.left);
}else{
stack.pop();
TreeNode temp = stack.pop();
if(pre != null && pre.val >= temp.val)
return false;
pre = temp;
}
}
return true;
}
}
```
```Java
class Solution {
// 递归
Expand Down
33 changes: 33 additions & 0 deletions problems/0530.二叉搜索树的最小绝对差.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,39 @@ class Solution {
}
}
```
統一迭代法-中序遍历
```Java
class Solution {
public int getMinimumDifference(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
int result = Integer.MAX_VALUE;

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);
stack.add(curr);
stack.add(null);
if(curr.left != null)
stack.add(curr.left);
}else{
stack.pop();
TreeNode temp = stack.pop();
if(pre != null)
result = Math.min(result, temp.val - pre.val);
pre = temp;
}
}
return result;
}
}
```

迭代法-中序遍历

```java
Expand Down