mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #725 from ironartisan/master
添加0530.二叉搜索树的最小绝对值差.md迭代Java解法
This commit is contained in:
@ -234,7 +234,15 @@ public:
|
||||
## Java
|
||||
|
||||
递归法:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
|
||||
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
迭代法:
|
||||
```java
|
||||
|
@ -293,6 +293,32 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```java
|
||||
// 解法2
|
||||
class Solution {
|
||||
public TreeNode deleteNode(TreeNode root, int key) {
|
||||
if (root == null) return root;
|
||||
if (root.val == key) {
|
||||
if (root.left == null) {
|
||||
return root.right;
|
||||
} else if (root.right == null) {
|
||||
return root.left;
|
||||
} else {
|
||||
TreeNode cur = root.right;
|
||||
while (cur.left != null) {
|
||||
cur = cur.left;
|
||||
}
|
||||
cur.left = root.left;
|
||||
root = root.right;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
if (root.val > key) root.left = deleteNode(root.left, key);
|
||||
if (root.val < key) root.right = deleteNode(root.right, key);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
||||
|
@ -429,6 +429,44 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
迭代法
|
||||
```java
|
||||
class Solution {
|
||||
public int[] findMode(TreeNode root) {
|
||||
TreeNode pre = null;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
List<Integer> result = new ArrayList<>();
|
||||
int maxCount = 0;
|
||||
int count = 0;
|
||||
TreeNode cur = root;
|
||||
while (cur != null || !stack.isEmpty()) {
|
||||
if (cur != null) {
|
||||
stack.push(cur);
|
||||
cur =cur.left;
|
||||
}else {
|
||||
cur = stack.pop();
|
||||
// 计数
|
||||
if (pre == null || cur.val != pre.val) {
|
||||
count = 1;
|
||||
}else {
|
||||
count++;
|
||||
}
|
||||
// 更新结果
|
||||
if (count > maxCount) {
|
||||
maxCount = count;
|
||||
result.clear();
|
||||
result.add(cur.val);
|
||||
}else if (count == maxCount) {
|
||||
result.add(cur.val);
|
||||
}
|
||||
pre = cur;
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
return result.stream().mapToInt(Integer::intValue).toArray();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
||||
|
@ -171,7 +171,34 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
迭代法-中序遍历
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
TreeNode pre;
|
||||
Stack<TreeNode> stack;
|
||||
public int getMinimumDifference(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
stack = new Stack<>();
|
||||
TreeNode cur = root;
|
||||
int result = Integer.MAX_VALUE;
|
||||
while (cur != null || !stack.isEmpty()) {
|
||||
if (cur != null) {
|
||||
stack.push(cur); // 将访问的节点放进栈
|
||||
cur = cur.left; // 左
|
||||
}else {
|
||||
cur = stack.pop();
|
||||
if (pre != null) { // 中
|
||||
result = Math.min(result, cur.val - pre.val);
|
||||
}
|
||||
pre = cur;
|
||||
cur = cur.right; // 右
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
## Python
|
||||
|
||||
递归
|
||||
|
Reference in New Issue
Block a user