783. Minimum Distance Between BST Nodes BST节点之间的最小距离

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

    def minDiffInBST(self, root):

        """

        :type root: TreeNode

        :rtype: int

        """

        res = float(‘inf‘)

        pre = -float(‘inf‘)

        def find(root):

            nonlocal res

            nonlocal pre

            if root.left:

                find(root.left)

            res = min(res, root.val - pre)

            pre = root.val

            if root.right:

                find(root.right)

        find(root)

        return res

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445817.html

时间: 2024-08-02 08:11:15

783. Minimum Distance Between BST Nodes BST节点之间的最小距离的相关文章

leetcode 783. Minimum Distance Between BST Nodes ---中序遍历

过年晚上无聊,233333333 题解: BST树的中序遍历是有序的,遍历过程中,记录前一个值,然后和当前值比较,来更新最小的minimum distance 注意python参数传递时候,像list这些object是传引用,单独int的数是传值的 void getans(TreeNode* root,int &pre,int &ans) { if(root==NULL) return; getans(root->left,pre,ans); if(pre!=INT_MAX) ans

783. Minimum Distance Between BST Nodes

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 objec

[LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes

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 objec

783. Minimum Distance Between BST Nodes - Easy

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 objec

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),而不是数组. 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / 2 6 / \ 1 3 最小的差

LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个不同节点的值之间的最小差值.示例: 给定的树[4,2,6,1,3,null,null]由下图表示: 4 / 2 6 / \ 1 3 输出:1 说明:请注意,root是TreeNode对象,而不是数组.该树中的任意节点最小差值为1,它发生在节点1和节点2之间,也发生在节点3和节点2之间. 注意: BS

编程之美 求二叉树中节点之间最大的距离

#include<iostream> using namespace std; //二叉树 节点结构 typedef struct TNODE_ { int data; struct TNODE_*left; struct TNODE_*right; }TNode; //获取树的高度=路径+1(最长路径经过的边数+1) int GetLRDistance(TNode*t) { int len=0; if(t==NULL) { return 0; }else{ int lenL=GetLRDis

STM32W108无线射频模块两节点之间通信实例

本文基于802.15.4/ZigBee的SimpleMac协议栈编写程序,实现两个STM32W108无线节点之间的通信.节点分为SUN节点和PLANET节点,SUN节点使用STM32W108无线开发板,PLANET节点使用STM32W108无线节点,SUN节点可与PC机进行通信. 程序设计与实现 程序的设计基于SimpleMac协议栈进行,根据官方提供的MAC协议栈示例代码进行的裁剪更改,第10章已对协议栈代码进行了解析,在此就不详细说明,以下只给出部分主要相关代码. 文件solar-syste

STM32W108无线射频模块多节点之间通信实例

STM32W108无线射频模块多节点之间通信实例 基于STM32W108的SimpleMac协议栈编写程序,实现多个无线节点之间的通信.节点分为SUN节点和PLANET节点,SUN节点使用STM32W108无线开发板,PLANET节点使用STM32W108无线数据采集节点,SUN节点可与PC机进行通信. 编程与实现 程序的设计基于SimpleMac协议栈进行,以下给出部分主要相关代码.该实例中的部分代码与第11章中的两节点通信实例代码相同,本章不再重复说明. 文件solar-system.c部分