mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0530.二叉搜索树的最小绝对差.md
添加 0530.二叉搜索树的最小绝对差 Java版本
This commit is contained in:
@ -151,7 +151,29 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```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:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user