给定一个二叉搜索树的根结点 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 最小的差值是 1, 它是节点1和节点2的差值, 也是节点3和节点2的差值。

注意:

  1. 二叉树的大小范围在 2 到 100。
  2. 二叉树总是有效的,每个节点的值都是整数,且不重复。
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
}; class Solution {
public:
int minDiffInBST(TreeNode* root)
{
if(root ->left != NULL && root ->right != NULL)
{
int res = min(abs(root ->val - SLeft(root ->left)), abs(root ->val - SRight(root ->right)));
return min(res, min(minDiffInBST(root ->right), minDiffInBST(root ->left)));
}
else if(root ->left == NULL && root ->right == NULL)
{
return INT_MAX;
}
else if(root ->left == NULL)
{
int res = abs(root ->val - SRight(root ->right));
return min(res, minDiffInBST(root ->right));
}
else
{
int res = abs(root ->val - SLeft(root ->left));
return min(res, minDiffInBST(root ->left));
}
} int SLeft(TreeNode* root)
{
if(root ->right == NULL)
return root ->val;
return SLeft(root ->right);
} int SRight(TreeNode* root)
{
if(root ->left == NULL)
return root ->val;
return SRight(root ->left);
}
};

Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离的更多相关文章

  1. [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

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

    783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...

  3. [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  4. [LC]783题 二叉搜索树结点最小距离(中序遍历)

    ①题目 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null]输出: 1解释:注意,root是树结点对象(T ...

  5. 【Leetcode_easy】783. Minimum Distance Between BST Nodes

    problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...

  6. BST(二叉搜索树)的基本操作

    BST(二叉搜索树) 首先,我们定义树的数据结构如下: public class TreeNode { int val; TreeNode left; TreeNode right; public T ...

  7. Java实现 LeetCode 783 二叉搜索树节点最小距离(遍历)

    783. 二叉搜索树节点最小距离 给定一个二叉搜索树的根节点 root,返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: ...

  8. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

  9. 783. Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

随机推荐

  1. light oj 1037 状压dp

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  2. python 日记 day1

    1.python2 与 python3 的区别:   a. python2 源码不标准,混乱,重复代码太多.默认方式是ascii码,解决方式:#-*- encoding:utf-8 -*-   b. ...

  3. 百钱白鸡(for循环的练习)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 新闻内页 上一篇写一篇问题,ID不连续,不用链表

    y要什么链表? 用sql查询上一篇 SELECT id,title FROM t_article WHERE id<10 ORDER BY id DESC LIMIT 1; 用sql查下一篇 S ...

  5. Navicat12激活,最新版本v12.1.18,原版激活[windows]

    1.navicat_premium原版安装包  :官网下载地址2.注册工具  :github地址 本次用到的软件我已经打包好  :    蓝奏云 我安装navicat的路径在:I:\Navicat P ...

  6. 新手必踩坑之display: inline-block

    今日励志语 往日不可追,来日犹可期,祝大家2019年继往开来 迷之间隙 我们创建一个导航列表,并将其列表 item 设置为 inline-block,主要代码如下: <div class=&qu ...

  7. ECMAScript 5 新增 Object 接口

    对象 构造器 说明 Object getPrototypeOf 返回对象的原型 Object getOwnPropertyDescriptor 返回对象自有属性的属性描述符 Object getOwn ...

  8. 如何理解张量tensor

    1 关于张量的四种定义 “张量”在不同的运用场景下有不同的定义. 第一个定义,张量是多维数组,这个定义常见于各种人工智能软件.听起来还好理解.--本文仅解释此种 2 多维数组 从第一个定义:张量是多维 ...

  9. linux系统之间互传文件

    参考网址:http://blog.csdn.net/shaoxiaohu1/article/details/23191637 1.文件复制:本机->远程服务器: scp /home/shaoxi ...

  10. call(this)自记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...