mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0530.二叉搜索树的最小绝对差.md
This commit is contained in:
@ -151,7 +151,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
递归
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
TreeNode pre;// 记录上一个遍历的结点
|
||||||
|
int result = Integer.MAX_VALUE;
|
||||||
|
public int getMinimumDifference(TreeNode root) {
|
||||||
|
if(root==null)return 0;
|
||||||
|
traversal(root);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public void traversal(TreeNode root){
|
||||||
|
if(root==null)return;
|
||||||
|
//左
|
||||||
|
traversal(root.left);
|
||||||
|
//中
|
||||||
|
if(pre!=null){
|
||||||
|
result = Math.min(result,root.val-pre.val);
|
||||||
|
}
|
||||||
|
pre = root;
|
||||||
|
//右
|
||||||
|
traversal(root.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
TreeNode pre;// 记录上一个遍历的结点
|
TreeNode pre;// 记录上一个遍历的结点
|
||||||
|
Reference in New Issue
Block a user