[Leetcode] 783. Minimum Distance Between BST Nodes 解题报告

LeetCode --- 783. Minimum Distance Between BST Nodes 解题分析 Given a Binary Search Tree (BST) with the root noderoot, returnthe 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, .. 阅读详情

题目:

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.

思路:

我们只需要对BST进行中序遍历,找出遍历中相邻节点之间差异的最小值即可。算法的空间复杂度是O(1),时间复杂度是O(n)。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDiffInBST(TreeNode* root) {
        if (root->left) {
            minDiffInBST(root->left);
        }
        if (first) {
            first = !first;
            pre_value = root->val;
        }
        else {
            ret = min(ret, root->val - pre_value);
            pre_value = root->val;
        }
        if (root->right) {
            minDiffInBST(root->right);
        }
        return ret;
    }
private:
    int ret = INT_MAX, pre_value = -1;
    bool first = true;
};
基于STM32的无人机智能避障与路径规划系统设计 本文详细介绍了基于STM32的无人机智能避障与路径规划系统设计,涵盖硬件选型、传感器组合、软件架构及算法实现。通过多传感器数据融合和改进的VFH+算法,系统在复杂环境中实现92%以上的避障成功率。STM32微控制器的实时性和低功耗特性为无人机自主导航提供了可靠保障,适用于工业巡检、仓储物流等场景。 阅读详情

相关推荐

PEMFC电化学入门:从电流密度到Tafel公式的实战计算指南

本文详细介绍了PEMFC电化学的基础知识,从电流密度计算到Tafel公式的应用,提供了实战计算指南。通过代码示例和常见问题排查,帮助工程师快速掌握燃料电池性能优化的关键参数,如交换电流密度和活化损失计算,适用于汽车动力和分布式发电等领域。

weixin_29229055的博客 552

leetcode 783. 二叉搜索树节点最小距离

一、题目 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。 输入:root = [4,2,6,1,3] 输出:1 提示: 树中节点数目在范围 [2, 100] 内 0 <= Node.val <= 105 二、解法 解法一:递归遍历、求左右子树的最值 思路: 对于root,找到它左子树的最大值和右子树的最小值,进行比较 /** * Definition for a binary tree node. * struct TreeNode { *

livingsu的博客 188

浙大数值分析lab2

最可怕的一道题,题目是这样的 Root of a Polynomial A polynomial of degree nnn has the common form as p(x)=cnxn+cn−1xn−1+⋯+c1x+c0p(x)=c_n x^n + c_{n-1} x^{n-1} + \cdots + c_1 x + c_0p(x)=c​n​​x​n​​+c​n−1​​x​

yao_zou的博客 4797

算法修炼之路—【字符串】Leetcode 783 二叉搜索树节点最小距离

文章目录题目描述思路分析解题代码复杂度分析小结Github源码 题目描述 给定一个二叉搜索树的根节点root,返回树中任意两节点的差的最小值。 示例1: 输入: root = [4, 2, 6, 1, 3, null, null] 输出: 1 解释: 注意,root是树节点对象(TreeNode object),而不是数组。给定的树可表示为下图: 最小的差值为1,它是节点1和节点2的差值,也是节点3和节点2的差值。 注意: 二叉树的大小范围在2到100; 二叉树总是有效的,每个节点的值都是整数,且不

技术更新每天 406

企业微信-审批控件中的外部选项的应用全流程完美解决方案

完美解决企业微信所有JS-SDK调用问题 完美解决苹果和PC端不适配的问题

qq_45195039的博客 5887

workbench拓扑优化教程_拓扑优化实例-轮毂的优化

拓扑优化实例-轮毂的优化ANSYS Workbench的新版本集成了topology optimization模块,为用户的拓扑优化提供的很好的应用。通过拓扑优化topology optimization模块的使用可以对拓扑有较好的理解,该模块拓扑优化可以简单概括为一句话:如何在合适的位置去除确定的材料使用量,而相应的刚度变化影响最小。本例以汽车轮毂为例,来确定轮毂的辐条形状。1.建立模型DM中可...

weixin_39581739的博客 3045

Leetcode-二叉搜索树中序遍历-783. 二叉搜索树节点最小距离

题目783. 二叉搜索树节点最小距离: 题解: 二叉搜索树中序遍历是有序的,因此最小差值一定出现在两个相邻的节点中 只要按照顺序遍历的时候,保存前一个节点的值与现在的值的差,与当前最小值做比较即可得到结果 注意:只要差值=1,我们就可以不继续往下看,直接返回 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * ...

一只菜鸟夹 309

个人信息保护合规审计人员(中级)能力测试试卷

本文是关于个人信息保护合规审计的考试指南,包含选择题、判断题、简答题和案例分析题。选择题考察《个人信息保护法》的关键条款,如敏感信息处理、跨境传输、委托处理等合规要求。判断题测试对个人信息处理规则的理解,如撤回同意、公开信息处理等。简答题要求阐述审计流程、个人权利、委托处理义务等内容。案例分析题以互联网企业为例,分析违规行为并提出整改建议,涉及数据收集、第三方共享、自动化决策等问题。全文旨在帮助中级审计人员掌握个人信息保护的法律规定和审计实务,提升合规审计能力。

qq_41525079的博客 549

LeetCode题解:783. 二叉搜索树节点最小距离,递归,JavaScript,详细注释

原题链接:783. 二叉搜索树节点最小距离 解题思路: 由于二叉搜索树是有序的,任意两个节点之差的最小值,即为相邻节点的值之差的最小值。 使用中序遍历二叉搜索树,即可从左到右按顺序读取每个值,再计算与上一个节点值的之差,同时取最小值即可。 /** * @param {TreeNode} root * @return {number} */ var minDiffInBST = function (root) { let min = Infinity // 记录最小值,初始值为Infinity,

Lee Chen的博客 268

LDRA Testbed软件静态分析_常见问题及处理

通过前面的几篇文章,LDRA Testbed软件静态分析的内容基本讲解完了,本篇文章将针对使用Testbed进行软件静态分析的常见问题进行介绍。 到此为止,LDRA Testbed软件静态分析的内容已经全部讲完,如果有问题,欢迎私信提问(不经常登录,回复可能不及时,见谅)。下一篇文章将开始讲解如何使用Testbed进行单元测试。

文章主要涉及嵌入式软件开发、测试、项目管理等,欢迎交流,共同进步。 2593

基于STM32L4XX、HAL库的 TMC2660C-PA 驱动程序设计

/ 默认灵敏度和电流。// 20/31最大电流。

zhusl6688的博客 386

LeetCode第 783 题:二叉搜索树节点最小距离(C++)

783. 二叉搜索树节点最小距离 - 力扣(LeetCode) 注意是任意两结点 中序遍历,记录中序遍历的前一个结点值就可以了: class Solution { public: int res = 110; int pre = -110;//记录中序遍历的前一个结点值 void inorder(TreeNode *root){ if(!root || res == 1) return; if(root->left){

zj 364

人工智能智能体研究综述:从理论架构到前沿应用

AI Agent,即人工智能智能体,是指能够在特定环境中自主感知、推理、决策并执行行动以实现特定目标的智能系统[1]。自主性反应性主动性(Pro-activeness)和社会性fill:#333;important;important;fill:none;color:#333;color:#333;important;fill:none;fill:#333;height:1em;AI Agent核心特征自主性 Autonomy反应性 Reactivity主动性 Pro-activeness。

nmdbbzcl的博客 904

【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)

【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/ 题目描述: Given a Binary Search...

负雪明烛 3020

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

LeetCode //C - 783. Minimum Distance Between BST Nodes

Problem Summary: Given a Binary Search Tree (BST), find the smallest difference between any two distinct node values. Solution Approach: The solution uses in-order traversal to visit nodes in ascending order. By keeping track of the previous node value dur

Made in Code 1698

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode 530. Minimum Absolute Difference in BST和783. Minimum Distance Between BST Nodes

【代码】leetcode 530. Minimum Absolute Difference in BST和783. Minimum Distance Between BST Nodes。

weixin_41554427的博客 402

783. Minimum Distance Between BST Nodes*

783. Minimum Distance Between BST Nodes* https://leetcode.com/problems/minimum-distance-between-bst-nodes/ 题目描述 Given a Binary Search Tree (BST) with the root node root, return the minimum difference ...

珍妮的选择的博客 357

Leetcode 783. Minimum Distance Between BST Nodes

示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(TreeNode object),而不是数组。 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / \ 2 6 / \ 1 3 最小的差值是 1,...

qiuyushuofeng的专栏 376

[leetcode] 783. Minimum Distance Between BST Nodes

Description 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,n...

Learning from the mistakes 150

leetcode#783. Minimum Distance Between BST Nodes

leetcode#783. Minimum Distance Between BST Nodes Problem Description Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two differen...

Faldict的博客 687

LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值。 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意: root 是树结点对象 (TreeNode object),而不是数组。 给定的树 ...

weixin_30597269的博客 144

783. Minimum Distance Between BST Nodes(递归)

package Recursion; public class MinDiffInBST_783 { // 783. Minimum Distance Between BST Nodes /* * Given a Binary Search Tree (BST) with the root node root, return the minimum * difference bet...

私念的博客 178

Leetcode 783.二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

Leetcode 783.二叉搜索树结点最小距离 1 题目描述(Leetcode题目链接)   给定一个二叉搜索树的根结点 root,返回树中任意两节点的差的最小值。 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(TreeNode object),而不是数组。 给定的树 [4,2,6,1,3,null,null] 可表示为下图:...

qq_39378221的博客 248

【Leetcode】783. Minimum Distance Between BST Nodes

题目地址: https://leetcode.com/problems/minimum-distance-between-bst-nodes/ 给定一个二叉搜索树,求其任意两节点的数值差的绝对值最小者。 思路是中序遍历,用一个变量存一下上次遍历到的节点值是多少,然后用当前节点减去上次遍历到的节点之差来更新答案。代码如下: public class Solution { private int res, prev; public int minDiffInBST(TreeN

数学、算法爱好者的博客 254

[LeetCode] 783. Minimum Distance Between BST Nodes

Given a Binary Search Tree (BST) with the root noderoot, returnthe minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,nu...

a0712104790的博客 83

LeetCode 783 Minimum Distance Between BST Nodes(easy)

题目链接:点击这里 若按中序遍历一棵二叉排序树,所得到的结点序列是一个递增序列。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),...

脚踏实地,仰望星空 206

LeetCode C++ 783. Minimum Distance Between BST Nodes【Binary Search Tree/Recursion】简单

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

memcpy0的博客 417
上一篇: [Leetcode] 782. Transform to Chessboard 解题报告
下一篇: [Leetcode] 784. Letter Case Permutation 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值