leetcode-17-BST
530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
解题思路:
先序遍历,存下来,然后排序,找临近两个的最小差值。
效率不高。。。后面再想想怎么改进???
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
get(root);
sort(nodes.begin(), nodes.end());
int Mini = abs(nodes[0]-nodes[1]);
for (int i = 1; i < nodes.size(); i++) {
Mini = min(Mini, abs(nodes[i]-nodes[i-1]));
}
return Mini;
}
void get(TreeNode* root) {
if (root)
nodes.push_back(root->val);
if (root->left)
get(root->left);
if (root->right)
get(root->right);
}
private:
vector<int> nodes;
};
235. Lowest Common Ancestor of a Binary Search Tree

解题思路:
直接找。。如果p,q的值大于root,就到右子树找;如果都小于root,就到左子树找,否则返回root。
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if (p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
return root;
}
leetcode-17-BST的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- [LeetCode] Split BST 分割二叉搜索树
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- Java实现 LeetCode 17 电话号码的字母组合
17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
- LeetCode - Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
随机推荐
- Linux 解压压缩war包
jar -xvf aaa.war (jar只能解压war 包到当前目录下) unzip aaa.war -d aaa/ (解压war包到aaa目录下) 打包aaa 下的所有文件为aaa.war ...
- 《javascript设计模式》笔记之第九章:组合模式
之前一直都是按照书的结构顺序做总结,觉得好像不是很好,现在试着完全按照自己的理解做总结.例子还是书上的例子. 一:组合模式的作用: 在web开发中,主要用于创建嵌套的html结点,使得我们方便的把各种 ...
- windows下利用intellij idea等工具开发erlang
今天突然想在家里去年新买的电脑上写点erlang代码,然后可耻的发现家里的电脑上竟然没有开发环境,果然是去年6月以后没写过erlang代码么╮(╯▽╰)╭? 首先下载需要用的几样东西: ...
- Echarts的重点
官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...
- 面向对象程序设计第四单元总结(UML系列)
2019面向对象程序设计第四单元总结 前言 本单元是面向对象程序设计课程的最后一个单元了,本单元是和UML模型相关,也就是说,我们需要正确理解UML模型的基础上,对构建出的UML模型进行解析,但是 ...
- java http的get,post请求
初学可用F12查看任意网页帮助理解 package httpTest: import java.io.BufferedReader; import java.io.IOException;import ...
- jQuery toggle 使用
jQuery 中 toggle 作用 切换元素的显示与隐藏状态 如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素. <body> <div class=" ...
- 【extjs6学习笔记】1.1 初始:创建项目
创建工作空间 sencha generate workspace /path/to/workspace 使用sencha创建应用 sencha -sdk /path/to/sdk generate a ...
- SCCM Collection 集合获取计算机最后启动时间
获取计算机客户端最后一次启动时间,我们可以通过多种来源获取,如活动目录组 ,而不仅仅是SCCM 收集,希望对您有所帮助,下面分享PowerShell 脚本 # 1 $CollectionName = ...
- Jmeter进行接口的性能测试
一.录制Jmeter脚本 录制Jmeter脚本有两种方法,一种是设置代理:一种则是利用badboy软件,badboy软件支持导出jmx脚本. 这里我们介绍第二种方法,利用badboy录制脚本,然后导出 ...