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的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  3. [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 ...

  4. [LeetCode] Split BST 分割二叉搜索树

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  7. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

  9. 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 ...

  10. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

随机推荐

  1. Exploring refit, an automatic type-safe REST library for .NET Standard

    自动类型安全的.NET标准REST库refit   在SCOTT HANSELMAN 博客上看到一个好东西<Exploring refit, an automatic type-safe RES ...

  2. mysql 无法启动 unkown command

    1. https://serverfault.com/questions/490656/mysql-not-starting-error-usr-sbin-mysqld-unknown-option- ...

  3. SqlDbx连接oracle

    解压SqlDbx.zip,将SqlDbx放到C:盘根目录 1.Path里面增加:C:\SqlDbx  Path是为了找tnsnames.ora 2.增加系统变量:ORACLE_HOME,路径:C:\S ...

  4. SmartWeatherAPI C#版

    private string GetKey(string areaId, string type, string date, string appId, string privateKey) { va ...

  5. 在东京生活的中国IT程序员

    应之前文章的博友邀请,我来开一篇在日本东京生活的中国IT程序员自谈,文中的讨论对象多为我自己或者是我的中国人(前)同事,有以偏概全之处还请包涵. 首先,我之前说日本的IT并不发达,不发达到什么程度呢? ...

  6. nuxt实践

    利用手脚架搭起来的服务端渲染实例目录结构.nuxtassets 未编译的静态资源如 LESS.SASS 或 JavaScriptcomponents 用于组织应用的 Vue.js 组件middlewa ...

  7. BZOJ3004: 吊灯(结论 毒瘤)

    题意 $n$个节点的树,判断能否划分成$\frac{n}{k}$个大小为$k$的联通块 Sol 首先$k$必须是$n$的倍数. 然后刚开始我就非常傻的以为输出所有约数就行了.. 但是图是这样,$k = ...

  8. Linux:linux下建ftp用户,并限制用户访问路径

    安装:ftp安装部分,操作步骤如下: 可以使用yum命令直接安装ftp # yum install vsftpd ftp服务的开启与关闭命令: 开启:# service vsftpd start 关闭 ...

  9. linux命令之文本查看

    vi掌握练习: 英文文档,相同的单词复制粘贴光标移动编辑等操作: cat:显示文件所有内容,小文件查看时使用. 缺点:文件大时不方便查看,文件很大时,会抢占系统资源,会出现命令崩溃. [zyj@loc ...

  10. db2数据库备份

    一.离线备份 db2  list  database  directory -----查看有哪些数据库,确定需要备份哪个数据库 db2  disconnect  current -----断开以数据库 ...