给定一个二叉搜索树的根结点 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. SQLServer-Version:SQLServer版本对应内部数据库版本号配置表

    ylbtech-SQLServer-Version:SQLServer版本对应内部数据库版本号配置表 1.返回顶部 1. 1.1 查询SQLServer对应的内部数据库版本号select DATABA ...

  2. ac68u、r8500 梅林固件扩展为一个小型 linux 系统

    事先刷 merlin 固件 1.安装 Entware 在安装之前,你需要在路由器中插入一个 U 盘,并将其格式化为 Ext3 / Ext4 格式,插上后在 /mnt/ 下应会多个 sda 设备出来 e ...

  3. PAT甲级——A1041 Be Unique

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  4. 利用GitHub来进行团队协作开发项目

    首先: 1.项目组长要在GitHub创建一个仓库 2.组长git clone仓库地址到本地   3.组长在本地克隆到的项目里面创建一个Django项目  4.在当前项目下进行git add以及git ...

  5. Simple implementation and results of genetic algorithm.

    This experiment was done for the final assignment of my Professional English class. This part has be ...

  6. vue 二维码长按保存和复制内容

    效果图: 二维码用了 qrcode.vue npm install qrcode.vue --save 复制内容用了 vue-clipboard2 npm install vue-clipboard2 ...

  7. Power Strings POJ2406 KMP 求最小循环节

    相比一般KMP,构建next数组需要多循环一次,因为next[j]代表前j-1个字符的最长相同前缀后缀,比如字符串为aab aab aab共9个字符,则next[10]等于前9个字符中最长相同前缀后缀 ...

  8. Docx 生成word文档二

    /// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...

  9. 【python之路34】面向对象作业之学生选课系统

    一.需求: 1.可以注册管理员账号,管理员账号可以创建老师和课程 2.学生可以注册和登陆,学生可以从课程列表选课,可以进行上课登记查看 二.代码 1.文件目录 bin 存放可执行文件 config 存 ...

  10. TZ_02MyBatis_lazy SqlMapConfig.xml

    Mybatis的延迟加载又称为懒加载 mybatis在一对多的查询中,例如查询一个用户时需要查询这个用户下的所有账户信息,如果一次性的select * from user u left join ac ...