前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 783 & 530 Distance Between BST Nodes

LeetCode 783 & 530 Distance Between BST Nodes

原创
作者头像
大学里的混子
修改2018-10-27 18:34:02
5320
修改2018-10-27 18:34:02
举报
文章被收录于专栏:LeetCodeLeetCode

783.Minimum Distance Between BST Nodes


Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.

Example :

代码语言:javascript
复制
Input: root = [4,2,6,1,3,null,null]
Output: 1
Explanation:
Note that root is a TreeNode object, not an array.

The given tree [4,2,6,1,3,null,null] is represented by the following diagram:

          4
        /   \
      2      6
     / \    
    1   3  

while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between
 node 3 and node 2.

解法一:

寻找到比root节点小的节点中的最大值,和比root节点大的节点中的最小值。通过这三个值的比较得出最小的diff,递归下去。此方法并没有完全利用到BST的特性,因此,效率不高。

代码语言:javascript
复制
public int minDiffInBST(TreeNode root) {
    if (root == null) return Integer.MAX_VALUE;
    int res = 0;
    int minL = Integer.MAX_VALUE;
    int minR = Integer.MAX_VALUE;
    if (root.left !=null){
        minL = root.val - BST_max(root.left).val;
    }
    if (root.right != null) {
        minR = BST_min(root.right).val - root.val;
    }
    res = Math.min(minL,minR);
    return  Math.min( res,Math.min(minDiffInBST(root.left),minDiffInBST(root.right)));
 }

 public TreeNode BST_min(TreeNode root){
    if (root.left == null) return root;
    return BST_min(root.left);
}

public TreeNode BST_max(TreeNode root){
    if (root.right == null) return root;
    return BST_max(root.right);
}

解法二:

我们知道,对于一个BST来说,中序遍历得到的顺序就是节点的从小到大的顺序排列的,因此可以采用一个变量prev,记录上一个节点的,采用当前节点的值减去上一个节点的值(prev),就可以得到相邻排序的两个节点的差值,一次循环下去,就可以方便的得出最小值。

代码语言:java
复制
     int minDiff = Integer.MAX_VALUE;
    TreeNode prev;
    public int minDiffInBST(TreeNode root) {
        int res = Integer.MAX_VALUE;
        inOrderBST(root);
        return minDiff;
    }
    
    public void inOrderBST(TreeNode root){
        if (root == null) return;
        inOrderBST(root.left);
        if (prev != null) minDiff = Math.min(minDiff,root.val - prev.val);
        prev = root;
        inOrderBST(root.right);
    }
  1. 注意res的初始化值,为Integer.MAX_VALUE
  2. 由于第一个节点(最小节点)之前,prev是null,因此,这里会有一个判断,只有当prev不为空的时候进行相减。

530. Minimum Absolute Difference in BST

解法:

代码语言:javascript
复制

Pick One
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Input:

        1
        \
          3
        /
        2

Output:
        1
Explanation:
代码语言:javascript
复制
TreeNode prev2 ;
int res = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
    if (root==null) return 0;
    getMinimumDifference(root.left);
    if (prev2!=null) res = Math.min(res,root.val - prev2.val);
    prev2 = root;
    getMinimumDifference(root.right);
    return res;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 783.Minimum Distance Between BST Nodes
    • 解法一:
      • 解法二:
      • 530. Minimum Absolute Difference in BST
        • 解法:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档