问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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

    /

   2

Output:

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)的更多相关文章

  1. [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. Leetcode:530. 二叉搜索树的最小绝对差

    Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...

  3. Java实现 LeetCode 530 二叉搜索树的最小绝对差(遍历树)

    530. 二叉搜索树的最小绝对差 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值. 示例: 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为 1,其中 2 ...

  4. [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)

    题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...

  5. [LC]530题 二叉搜索树的最小绝对差

    ①题目 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值. 示例 : 输入: 1   \   3  / 2 输出:1 解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  6. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  7. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  8. LeetCode530. 二叉搜索树的最小绝对差

    题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...

  9. 【leetcode_easy】530. Minimum Absolute Difference in BST

    problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...

  10. [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 ...

随机推荐

  1. SpringCloud全家桶介绍及手绘架构

    文章目录 概述 一.业务场景介绍 二.Spring Cloud核心组件:Eureka 三.Spring Cloud核心组件:Feign 四.Spring Cloud核心组件:Ribbon 五.Spri ...

  2. Babel:下一代Javascript语法编译器

    定义 Babel是一个Javascript的编译器,通过它你可以将一些新版本的ECMAScript语法转换成低版本的语法.以便能够在低版本的浏览器或者其它环境平稳运行. 截至目前笔者写这篇文章的时候, ...

  3. 查看进程中占cpu高的线程方法

    当在任务管理器中发现有进程占用cpu过高的时候通过下面的指令将进程快照导出到c盘 jstack -l 进程PID > c:/进程PID.stack  (此命令生成.stack文件在c盘中,用文本 ...

  4. Ethical Hacking - NETWORK PENETRATION TESTING(23)

    Detecting ARP Posionning Attacks ARP main security issues: 1. Each ARP requests/response is trusted. ...

  5. 013.Nginx动静分离

    一 动静分离概述 1.1 动静分离介绍 为了提高网站的响应速度,减轻程序服务器(Tomcat,Jboss等)的负载,对于静态资源,如图片.js.css等文件,可以在反向代理服务器中进行缓存,这样浏览器 ...

  6. 7.20试机测 T3 阶乘之和 暴力AC题解

    7.20试机测  T3 阶乘之和 暴力AC题解 题外话:此乃本蒟蒻发表的第一篇题解,大家多多关照,支持一下,谢谢 题面 3.阶乘之和(sum.pas/in/out) 问题描述: 给定一个非负整数 n, ...

  7. android 6.0三星5.1.1Root

    现在google是越来越不给我们留活路了… 从android 6.0开始, 三星的5.1.1开始. 默认都开启了data分区的forceencryption, 也就是强制加密. 也开启了/system ...

  8. std:ios:sync_with_stdio (false)以及局限性

    如何在输入输出上提高一下效率emmmm #include<iostream> #include<stdio.h> #include<stdlib.h> #inclu ...

  9. Python Tuple(元组) min()方法

    描述 Python 元组 min() 函数返回元组中元素最小值.高佣联盟 www.cgewang.com 语法 min()方法语法: min(tuple) 参数 tuple -- 指定的元组. 返回值 ...

  10. PHP pclose() 函数

    定义和用法 pclose() 函数关闭由 popen() 打开的进程. 如果失败,该函数返回 FALSE. 语法 pclose(pipe) 参数 描述 pipe 必需.规定由 popen() 打开的进 ...