C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4123 访问。
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。
输入:
1
\
3
/
2输出:
1
解释:
最小绝对差为1,其中 2 和 1 的差的绝对值为 1(或者 2 和 3)。
注意: 树中至少有2个节点。
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Input:
1
\
3
/
2Output:
1
Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
Note: There are at least two nodes in this BST.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4123 访问。
public class Program {
public static void Main(string[] args) {
var root = new TreeNode(4) {
left = new TreeNode(8),
right = new TreeNode(1)
};
var res = GetMinimumDifference(root);
Console.WriteLine(res);
Console.ReadKey();
}
public static int GetMinimumDifference(TreeNode root) {
var list = new List<int>();
PreOrder(root, ref list);
var res = int.MaxValue;
list.Sort();
for(var i = 0; i < list.Count - 1; i++) {
res = Math.Min(res, list[i + 1] - list[i]);
}
return res;
}
public static void PreOrder(TreeNode root, ref List<int> list) {
if(root == null) return;
list.Add(root.val);
PreOrder(root?.left, ref list);
PreOrder(root?.right, ref list);
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4123 访问。
3
分析:
对于该题来说,虽然使用了运行库 list.Sort() 排序,但 GetMinimumDifference 方法中最耗时且最重要的步骤是 res = Math.Min(res, list[i + 1] - list[i]) 这一句代码,所以我认为以上算法的时间复杂度应当为: 。
C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)的更多相关文章
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- Leetcode:530. 二叉搜索树的最小绝对差
Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...
- Java实现 LeetCode 530 二叉搜索树的最小绝对差(遍历树)
530. 二叉搜索树的最小绝对差 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值. 示例: 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为 1,其中 2 ...
- [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...
- [LC]530题 二叉搜索树的最小绝对差
①题目 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值. 示例 : 输入: 1 \ 3 / 2 输出:1 解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- LeetCode530. 二叉搜索树的最小绝对差
题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- LeetCode 84 | 单调栈解决最大矩形问题
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第52篇文章,我们一起来看LeetCode第84题,Largest Rectangle in Histogram( ...
- 细说php锁
bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件指针.o ...
- Python如何向SQLServer存储二进制图片
需求是需要用python往 SqlServer中的image类型字段中插入二进制图片 核心代码,研究好几个小时的代码: 安装pywin32,adodbapi image_url = "图片链 ...
- WinForm微信扫码登录
源码还需优化,不喜勿喷. 微信官方文档 : https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat ...
- Arrays.sort() ----- DualPivotQuicksort
Arrays.sort() ----- DualPivotQuicksort DualPivotQuicksort是Arrays.sort()对基本类型的排序算法,它不止使用了双轴快速排序,还使用了T ...
- Java Web(1)-JavaScript
一.JavaScript 和 html 代码的结合方式 1. 第一种方式 只需要在 head 标签中,或者在 body 标签中, 使用 script 标签 来书写 JavaScript 代码 < ...
- 学python不是一个入门教程就行,学之前你必须知道这些
第一 学习方向:python应用方向很多,必须明确学习方向想 全栈开发:就是web应用开发,是用来写网站后台的数据分析:就是做大数据分析的,数据量级起码是千万级别的,做的是大数据分析网络爬虫:其实属于 ...
- 通过C语言获取MAC地址(转)
原文:http://blog.chinaunix.net/uid-25885064-id-3125167.html #include <sys/ioctl.h>#include <n ...
- Spring Security 实战干货:从零手写一个验证码登录
1. 前言 前面关于Spring Security写了两篇文章,一篇是介绍UsernamePasswordAuthenticationFilter,另一篇是介绍 AuthenticationManag ...
- Python网络编程基础 PDF 完整超清版|网盘链接内附提取码下载|
点此获取下载地址提取码:y9u5 Python网络编程最好新手入门书籍!175个详细案例,事实胜于雄辩,Sockets.DNS.Web Service.FTP.Email.SMTP.POP.IMAP. ...