Weekly Contest 71 leetcode 783. Minimum Distance Between BST Nodes

257 篇文章 17 订阅

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 :

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.

Note:

  1. The size of the BST will be between 2 and 100.
  2. The BST is always valid, each node's value is an integer, and each node's value is different.

这道题蛮简单的。

	public int minDiffInBST(TreeNode root) {
		List<Integer> list=new ArrayList<Integer>();
		dfs(root, list);
		Collections.sort(list);
		int min=Integer.MAX_VALUE;
		for(int i=0;i<list.size()-1;i++){
			int distance=list.get(i+1)-list.get(i);
			min=Math.min(min, distance);
		}
		return min;
	}
	
	public void dfs(TreeNode node,List<Integer> list){
		if(node!=null){
			list.add(node.val);
			dfs(node.left, list);
			dfs(node.right, list);
		}
	}
这道题有solutions: https://leetcode.com/problems/minimum-distance-between-bst-nodes/solution/
我上面的方法复杂度是  O ( N log N ),solutions中给出了一个的  O ( N 方法。
我这才发现,原来题目中是个BST(二叉搜索树),看来要好好读题啊。。。

在二叉搜索树中,中根遍历会按从小到大的顺序输出结点值。可以使用这个性质。

class Solution {
    Integer prev, ans;
    public int minDiffInBST(TreeNode root) {
        prev = null;
        ans = Integer.MAX_VALUE;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode node) {
        if (node == null) return;
        dfs(node.left);
        if (prev != null)
            ans = Math.min(ans, node.val - prev);
        prev = node.val;
        dfs(node.right);
    }
}

Complexity Analysis

  • Time Complexity: O(N)O(N), where NN is the number of nodes in the tree. We iterate over every node once.

  • Space Complexity: O(H)O(H), where HH is the height of the tree. This is the space used by the implicit call stack when calling dfs.

或者就直接按照中根遍历,把结点加到 list 中,最后再对 list 中的元素 一对一对 地比较。
class Solution {
    int minDiff = Integer.MAX_VALUE;
    public int minDiffInBST(TreeNode root) {
        List<TreeNode> list = new LinkedList<TreeNode>();
        inorder(root, list);
        for(int i = 1; i < list.size(); i++)
            minDiff = Math.min(minDiff, Math.abs((list.get(i).val)-(list.get(i-1).val)));
        return minDiff;
    }
    private void inorder(TreeNode root, List<TreeNode> list){
        if(root==null) return;
        inorder(root.left, list);
        list.add(root);
        inorder(root.right, list);
    }
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值