https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/
和
https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
解题思路:
- 相同的解题思路因为left < root < right, 因此用inorder travesal
代码:
class Solution {
int diff = Integer.MAX_VALUE;
TreeNode pre = null;
public int minDiffInBST(TreeNode root) {
inOrder(root);
return diff;
}
public void inOrder(TreeNode root){
if(root == null) return;
inOrder(root.left);
if(pre != null) diff = Math.min(diff, root.val - pre.val);
pre = root;
inOrder(root.right);
}
}
网友评论