美文网首页
783. Minimum Distance Between BS

783. Minimum Distance Between BS

作者: becauseyou_90cd | 来源:发表于2018-07-31 04:15 被阅读0次

https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/

https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
解题思路:

  1. 相同的解题思路因为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);
}

}

相关文章

网友评论

      本文标题:783. Minimum Distance Between BS

      本文链接:https://www.haomeiwen.com/subject/pjfcvftx.html