给定一个二叉搜索树的根结点 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. 玩转python爬虫之正则表达式

    玩转python爬虫之正则表达式 这篇文章主要介绍了python爬虫的正则表达式,正则表达式在Python爬虫是必不可少的神兵利器,本文整理了Python中的正则表达式的相关内容,感兴趣的小伙伴们可以 ...

  2. python intern(字符串驻留机制)

    python 中为了提高字符串的使用用效率和节约内存,对于由 字母.数字.下划线组成的标识符采用了 intern 机制,即对于短字符串,将其赋值给多个对象时,内存中只有一个副本,多个对象共享这个副本. ...

  3. mysql api 不支持source命令

    今天写了个代码 <?php ..... mysql_query("source /tmp/cr.sql",$link); ..... ?> 结果死活cr.sql的SQL ...

  4. mybatis学习:mybatis的注解开发和编写dao实现类的方式入门

    一.使用注解则不需要创建映射配置文件:即xxxDao.xml javaBean为什么要实现Serializable接口? Java的"对象序列化"能让你将一个实现了Serializ ...

  5. os.path.join 的用法

    Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...

  6. UVA11389 The Bus Driver Problem

        题意:有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d*r.问如何分配路线才能使加班费最少.   贪心 ...

  7. php获取数据转换成json格式

    <?php header("content-type:text/html;charset=utf-8"); $con=mysql_connect("localhos ...

  8. Hosts 广告

    # 百度 127.0.0.1 cpro.baidustatic.com 127.0.0.1 dup.baidustatic.com 127.0.0.1 hm.baidu.com 127.0.0.1 i ...

  9. android搭建

    搭建:https://www.cnblogs.com/zoupeiyang/p/4034517.html#1 android sdk manager 翻墙:http://www.androiddevt ...

  10. 学习Web前端开发时有哪些技术点

    现在学前端的人是越来越多,学习质量也是参差不齐.过来人的身份告诉你,如果你还没有下定决心花时间去学习Web前端,那也可以先找些视频学习下,Web前端开发有哪些常见技术点!接下来,就看看Web前端开发有 ...