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 ...
随机推荐
- 题解 CF296B 【Yaroslav and Two Strings】
题目 传送门 题目大意 如果两个只包含数字且长度为 \(n\) 的字符串 \(s\) 和 \(w\) 存在两个数字 \(1≤i,j≤n\),使得 \(s_i<w_i,s_j>w_j\) , ...
- Pollard-Rho
\(code:\) ll pri[12]={2,3,5,7,11,13,17,19}; ll mul(ll x,ll y,ll mod) { ll c=(long double)x*y/mod+0.5 ...
- LeetCode 85 | 如何从矩阵当中找到数字围成的最大矩形的面积?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题53篇文章,我们一起来看看LeetCode中的85题,Maximal Rectangle(最大面积矩形). 今天的 ...
- Nginx配置多个域名指向不同的端口
一.前言 很多时候我们都会在同一个服务器上部署多个项目,我们有多个域名的情况下,怎么样使不同的域名都通过80端口来访问呢?打个比方说,现在有2个域名,分别是:tessai.cn 和 admin.tes ...
- R 基础绘图体系-基础篇
1.高水平绘图函数 生成数据 #模拟100位同学学号及三科成绩 num = seq(12340001,12340100) # 形成学号 x1 = round(runif(100,min = 80,ma ...
- 机器学习 | 简介推荐场景中的协同过滤算法,以及SVD的使用
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第29篇文章,我们来聊聊SVD在上古时期的推荐场景当中的应用. 推荐的背后逻辑 有没有思考过一个问题,当我们在淘宝或者是 ...
- Day08_商品规格管理
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...
- Python核心编程(第3版)PDF高清晰完整中文版|网盘链接附提取码下载|
一.书籍简介<Python核心编程(第3版)>是经典畅销图书<Python核心编程(第二版)>的全新升级版本.<Python核心编程(第3版)>总共分为3部分.第1 ...
- Python元组索引、截取
Python元组索引.截取: 索引下标: tuple_1 = ('a','b','c','d','e','f','g','h') print(tuple_1[0]) # a print(tuple_1 ...
- Django学习路23_if else 语句,if elif else 语句 forloop.first第一个元素 .last最后一个元素,注释
if else 格式 {% if 条件 %} <标签>语句</标签> {%else%} <标签>语句</标签> {%endif} 标签都可以添加样式 { ...