mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
更新0530.二叉搜索树的最小绝对差
This commit is contained in:
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
> 利用二叉搜索树的特性搞起!
|
> 利用二叉搜索树的特性搞起!
|
||||||
|
|
||||||
## 530.二叉搜索树的最小绝对差
|
# 530.二叉搜索树的最小绝对差
|
||||||
|
|
||||||
题目地址:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/
|
题目地址:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/
|
||||||
|
|
||||||
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
提示:树中至少有 2 个节点。
|
提示:树中至少有 2 个节点。
|
||||||
|
|
||||||
## 思路
|
# 思路
|
||||||
|
|
||||||
题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。
|
题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ public:
|
|||||||
|
|
||||||
## 迭代
|
## 迭代
|
||||||
|
|
||||||
看过这两篇[二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/c_zCrGHIVlBjUH_hJtghCg),[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://mp.weixin.qq.com/s/WKg0Ty1_3SZkztpHubZPRg)文章之后,不难写出两种中序遍历的迭代法。
|
看过这两篇[二叉树:听说递归能做的,栈也能做!](https://mp.weixin.qq.com/s/OH7aCVJ5-Gi32PkNCoZk4A),[二叉树:前中后序迭代方式统一写法](https://mp.weixin.qq.com/s/ATQMPCpBlaAgrqdLDMVPZA)文章之后,不难写出两种中序遍历的迭代法。
|
||||||
|
|
||||||
下面我给出其中的一种中序遍历的迭代法,代码如下:
|
下面我给出其中的一种中序遍历的迭代法,代码如下:
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## 总结
|
# 总结
|
||||||
|
|
||||||
**遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。**
|
**遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。**
|
||||||
|
|
||||||
@ -142,15 +142,11 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
|
## Java
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
|
||||||
|
|
||||||
|
|
||||||
Java:
|
|
||||||
递归
|
递归
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -175,38 +171,11 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
```Java
|
|
||||||
class Solution {
|
|
||||||
TreeNode pre;// 记录上一个遍历的结点
|
|
||||||
int result = Integer.MAX_VALUE;
|
|
||||||
public int getMinimumDifference(TreeNode root) {
|
|
||||||
if (root == null) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
// 左
|
|
||||||
int left = getMinimumDifference(root.left);
|
|
||||||
|
|
||||||
// 中
|
|
||||||
if (pre != null) {
|
|
||||||
result = Math.min(left, root.val - pre.val);
|
|
||||||
}
|
|
||||||
pre = root;
|
|
||||||
// 右
|
|
||||||
int right = getMinimumDifference(root.right);
|
|
||||||
result = Math.min(right, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Python:
|
## Python
|
||||||
|
|
||||||
|
递归
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
|
||||||
# class TreeNode:
|
|
||||||
# def __init__(self, val=0, left=None, right=None):
|
|
||||||
# self.val = val
|
|
||||||
# self.left = left
|
|
||||||
# self.right = right
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def getMinimumDifference(self, root: TreeNode) -> int:
|
def getMinimumDifference(self, root: TreeNode) -> int:
|
||||||
res = []
|
res = []
|
||||||
@ -222,8 +191,10 @@ class Solution:
|
|||||||
for i in range(len(res)-1): // 统计有序数组的最小差值
|
for i in range(len(res)-1): // 统计有序数组的最小差值
|
||||||
r = min(abs(res[i]-res[i+1]),r)
|
r = min(abs(res[i]-res[i+1]),r)
|
||||||
return r
|
return r
|
||||||
|
```
|
||||||
# 迭代法-中序遍历
|
|
||||||
|
迭代法-中序遍历
|
||||||
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def getMinimumDifference(self, root: TreeNode) -> int:
|
def getMinimumDifference(self, root: TreeNode) -> int:
|
||||||
stack = []
|
stack = []
|
||||||
@ -242,19 +213,13 @@ class Solution:
|
|||||||
cur = cur.right
|
cur = cur.right
|
||||||
return result
|
return result
|
||||||
|
|
||||||
```
|
```
|
||||||
Go:
|
|
||||||
> 中序遍历,然后计算最小差值
|
## Go:
|
||||||
|
|
||||||
|
中序遍历,然后计算最小差值
|
||||||
|
|
||||||
```go
|
```go
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
|
||||||
* type TreeNode struct {
|
|
||||||
* Val int
|
|
||||||
* Left *TreeNode
|
|
||||||
* Right *TreeNode
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
func getMinimumDifference(root *TreeNode) int {
|
func getMinimumDifference(root *TreeNode) int {
|
||||||
var res []int
|
var res []int
|
||||||
findMIn(root,&res)
|
findMIn(root,&res)
|
||||||
@ -299,7 +264,7 @@ func getMinimumDifference(root *TreeNode) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript版本
|
## JavaScript
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user