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.

解题思路:

先序遍历,存下来,然后排序,找临近两个的最小差值。

效率不高。。。后面再想想怎么改进???

  1. class Solution {
  2. public:
  3. int getMinimumDifference(TreeNode* root) {
  4. get(root);
  5. sort(nodes.begin(), nodes.end());
  6. int Mini = abs(nodes[0]-nodes[1]);
  7. for (int i = 1; i < nodes.size(); i++) {
  8. Mini = min(Mini, abs(nodes[i]-nodes[i-1]));
  9. }
  10. return Mini;
  11. }
  12. void get(TreeNode* root) {
  13. if (root)
  14. nodes.push_back(root->val);
  15. if (root->left)
  16. get(root->left);
  17. if (root->right)
  18. get(root->right);
  19. }
  20. private:
  21. vector<int> nodes;
  22.  
  23. };

235. Lowest Common Ancestor of a Binary Search Tree

解题思路:

直接找。。如果p,q的值大于root,就到右子树找;如果都小于root,就到左子树找,否则返回root。

  1. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  2. if (p->val < root->val && q->val < root->val)
  3. return lowestCommonAncestor(root->left, p, q);
  4. if (p->val > root->val && q->val > root->val)
  5. return lowestCommonAncestor(root->right, p, q);
  6. return root;
  7. }

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. Python入门_汇总_未完待续

    if/elseif/else for while break continue 多重循环 list [] duple() dict {} set {[]} 函数 help(abs) 查看abs函数的帮 ...

  2. net Core 入门实战

    Asp.net Core 入门实战   Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone 目录 快速入门 安 ...

  3. javac 找不到文件 的可能原因

    初学Java还不太明白,竟在些简单的事情上栽跟头,分享一下省的麻烦. 当我们配置好JDK和环境变量之后,在命令行下输入javac,说明我们的安装是正确的.

  4. unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计

    unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计 最近做玩票性质的游戏项目,客户端技术是 unity3d 和 android. ...

  5. 基于JAVA的设计模式之代理模式

    概念 王宝强有一个经纪人叫宋喆,这个经纪人很吊,可以代理王宝强做一系列的事情,与粉丝活动.王宝强的微博维护.安排王宝强的行程以及什么什么等等.如果王宝强一个人做岂不是累死.通过这个代理人为王宝强节省了 ...

  6. Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法

    菜鸟日记: 1:new web project: 2:fix the visiting  path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...

  7. 洛谷P1435 回文字串(dp)

    题意 题目链接 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “Ab3bd”插入2个字符后可 ...

  8. springBoot jpa 表单关联查询

    1.创建两个实体类 import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.per ...

  9. JavaScript空假值及其判断

    一.javaScript五种空值和假值 分别为undefined,null,false,"",0,这五个值的共同点是在执行if语句时都会执行false分支,执行对应的非语句的时候都 ...

  10. Python-OpenCV——Image Blurring(Image Smoothing)

    通过将图像与低通滤波器内核卷积来实现图像模糊.它有助于消除噪音.它实际上从图像中去除了高频内容(例如:噪声,边缘).因此在此操作中边缘会有点模(嗯,有模糊技术,也不会模糊边缘). OpenCV主要提供 ...