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< ...
随机推荐
- java颜色代码对照表
LightPink 浅粉色 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 #DC143C 220,20,60 LavenderB ...
- C++ 11 Lambda表达式!!!!!!!!!!!
C++11的一大亮点就是引入了Lambda表达式.利用Lambda表达式,可以方便的定义和创建匿名函数.对于C++这门语言来说来说,“Lambda表达式”或“匿名函数”这些概念听起来好像很深奥,但很多 ...
- MDX分页查询
WITH SET [e16a30d0-2174-4874-8dae-a5085a75a3e2] as NONEMPTY({[Measures].[终端销售数量], [Measures].[终端销售吊牌 ...
- 机器学习框架ML.NET学习笔记【6】TensorFlow图片分类
一.概述 通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下: 之前介绍 ...
- 路径方案数(mod)
路径方案数(mod) [题目描述] 给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点, cyb 可以从 a 走到 b,当且仅当a到2的最短路,比b 到2的最短路长. 求 cy ...
- Vue2之页面 、js 、css分离
在编写vue的时候,页面数据少的时候,可以将所有的js和css都可以直接写在页面上,但是页面数据多,js.css的方法和样式多的时候,都放在一个页面的时候,就显得页面vue十分的臃肿. 所以写项目的时 ...
- SPRING代理模式
1.静态代理 主题对象:Student public interface Student { public String add(); } 目标对象:RealStudent public class ...
- [转]兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP
一.方案确定 计划做视频播放,要求能够播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...
- JsonModel&AFNetWorking
// HttpManager.h // JsonModel&AFNetWorking // // Created by qianfeng on 15/7/21. // Copyright (c ...
- cocos2dx贝塞尔曲线--使用PS辅助规划动作路径
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ...