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< ...
随机推荐
- Python入门_汇总_未完待续
if/elseif/else for while break continue 多重循环 list [] duple() dict {} set {[]} 函数 help(abs) 查看abs函数的帮 ...
- net Core 入门实战
Asp.net Core 入门实战 Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone 目录 快速入门 安 ...
- javac 找不到文件 的可能原因
初学Java还不太明白,竟在些简单的事情上栽跟头,分享一下省的麻烦. 当我们配置好JDK和环境变量之后,在命令行下输入javac,说明我们的安装是正确的.
- unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计
unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计 最近做玩票性质的游戏项目,客户端技术是 unity3d 和 android. ...
- 基于JAVA的设计模式之代理模式
概念 王宝强有一个经纪人叫宋喆,这个经纪人很吊,可以代理王宝强做一系列的事情,与粉丝活动.王宝强的微博维护.安排王宝强的行程以及什么什么等等.如果王宝强一个人做岂不是累死.通过这个代理人为王宝强节省了 ...
- Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法
菜鸟日记: 1:new web project: 2:fix the visiting path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...
- 洛谷P1435 回文字串(dp)
题意 题目链接 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “Ab3bd”插入2个字符后可 ...
- springBoot jpa 表单关联查询
1.创建两个实体类 import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.per ...
- JavaScript空假值及其判断
一.javaScript五种空值和假值 分别为undefined,null,false,"",0,这五个值的共同点是在执行if语句时都会执行false分支,执行对应的非语句的时候都 ...
- Python-OpenCV——Image Blurring(Image Smoothing)
通过将图像与低通滤波器内核卷积来实现图像模糊.它有助于消除噪音.它实际上从图像中去除了高频内容(例如:噪声,边缘).因此在此操作中边缘会有点模(嗯,有模糊技术,也不会模糊边缘). OpenCV主要提供 ...