链接:https://leetcode.com/tag/binary-search-tree/

【220】Contains Duplicate III (2019年4月20日) (好题)

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

Example 1:
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1, t = 2
Output: true
Example 3:
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false

题解:

解法1. brute force。  O(NK)

解法2. set + lower_bound(), sliding window, time complexity: O(NlogK), space O(K)

解法3. bucket:unordered_map<int, int> : key bucket idx, value nums[i], time complexity: O(N), space: O(K)

 class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (k <= || t < ) {return false;}
set<int> st;
for (int i = ; i < nums.size(); ++i) {
int num = nums[i];
auto iter = st.lower_bound(num);
if (iter != st.end()) {
int greater = *iter;
if ((long)greater - num <= t) {return true;}
}
if (iter != st.begin()) {
--iter;
int less = *iter;
if ((long)num - less <= t) {return true;}
}
if (st.size() == k) {
st.erase(nums[i-k]);
}
st.insert(num);
}
return false;
}
};

solution2

 class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.empty() || k <= || t < ) {return false;}
long bucketSize = (long)t + ;
int minn = nums.front();
for (auto& num : nums) {
minn = min(num, minn);
}
unordered_map<long, int> bucket; //key: bucketIdx, value: nums[i]
for (int i = ; i < nums.size(); ++i) {
long bucketIdx = ((long)nums[i] - minn) / bucketSize;
if (bucket.count(bucketIdx)) {return true;}
int left = bucketIdx - , right = bucketIdx + ;
if (bucket.count(left) && (long)nums[i] - bucket[left] <= t) {return true;}
if (bucket.count(right) && (long)bucket[right] - nums[i] <= t) {return true;}
if (i >= k) {
long removeKey = ((long)nums[i-k] - minn) / bucketSize;
bucket.erase(removeKey);
}
bucket[bucketIdx] = nums[i];
}
return false;
}
};

solution3

【315】Count of Smaller Numbers After Self

【327】Count of Range Sum

【352】Data Stream as Disjoint Intervals

【493】Reverse Pairs

【530】Minimum Absolute Difference in BST (2019年3月10日)(Easy)

返回一棵 BST的两个结点的最小绝对值的距离之差。

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

题解:根据 BST 的性质,我们只需要用一个变量记录中序遍历的前一个结点prev即可。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
if (!root) {return ;}
inorder(root);
return res;
}
int res = INT_MAX;
TreeNode* prev = nullptr;
void inorder(TreeNode* root) {
if (!root) {return;}
inorder(root->left);
if (prev) {
res = min(res, root->val - prev->val);
}
prev = root;
inorder(root->right);
return;
}
};

【683】K Empty Slots

【699】Falling Squares

【715】Range Module

【731】My Calendar II (2019年3月21日)

题意是每次给定一个 event  [start, end) ,如果插入这个 event 之后, 当前有个时刻的同时进行的 event 的数量大于等于 3,那么就不插入这个 event,返回这个 event 能不能被插入。

此题只要找出所有与[start,end)重合的区间,再检查这些区间是否有互相的重合。是的话,说明必然有triple booking。

【732】My Calendar III (2019年3月21日)

题意是每次插入一个 event,返回插入这个event之后,对于任意一个时刻,同时在进行的 event 有多少个。

https://leetcode.com/problems/my-calendar-iii/description/

sweep line 的思想,用一个 multiset,记录 event 和 event 类型,如果是 start,event就表示成{start, 1}, 如果是 end,event 就表示成 {end, -1},然后每次插入之后,去遍历 multiset,如果碰到 1, 就 +1, 如果碰到 -1 ,就 -1。

【776】Split BST

【783】Minimum Distance Between BST Nodes (2019年3月10日)(Easy)

返回一棵 BST的两个结点的最小的距离之差。

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.

题解:同上面 530 题,一样的解法。

【938】Range Sum of BST

【LeetCode】二叉查找树 binary search tree(共14题)的更多相关文章

  1. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  2. [leetcode]Recover Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...

  3. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  4. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. [Leetcode] Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  7. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

  8. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  9. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

随机推荐

  1. 170817关于Listener的知识点

    1.  Listener   监听器简介                    Listener是JavaWeb中三大组件之一.Servlet.Filter.Listener              ...

  2. 设计模式之动态代理(Java的JDK动态代理实现)

    先来看一下思维导图: 对于JDK的动态代理,孔浩老师说学习的方法是把它记下来. 先写一个主题接口类,表示要完成的一个主题. package com.liwei.dynaproxy; /** * 要代理 ...

  3. HTTP入门(二):用Chrome开发者工具查看 HTTP 请求与响应

    HTTP入门(二):用Chrome开发者工具查看 HTTP 请求与响应 本文简单总结HTTP的请求与响应. 本文主要目的是对学习内容进行总结以及方便日后查阅. 详细教程和原理可以参考HTTP文档(MD ...

  4. MySQL多表查询合并结果union all,内连接查询

    MySQL多表查询合并结果和内连接查询 1.使用union和union all合并两个查询结果:select 字段名 from tablename1 union select 字段名 from tab ...

  5. Unity各版本差异

    Unity各版本差异 version unity 5.x 4.x  2017 差异 特点  首先放出unity的下载地址,然后再慢慢分析各个版本.再者unity可以多个版本共存,只要不放在同一目录下. ...

  6. 在mpvue或者Vue中使用VUEX

    第一个页面Index,主页哦 import Vue from'vue' import Vuex from'vuex' import getters from'./getters' import sta ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_2_字符输入流读取字符数据

    读取的文件有中文也有英文 强转为char类型 缓冲读取多个字符 使用string的构造方法转换为字符输出

  8. 使用pyautogui替代selenium,图像识别进行web自动化测试--基于python语言

    这里演示一下使用pyautogui替代selenium进行web自动化测试,并不建议使用pyautogui进行web自动化测试,因为元素的ui一旦有长宽变化,或者风格的变化,执行时就会发生异常,仅当学 ...

  9. jmeter逻辑控制器详解(2)

    逻辑控制器 8.Runtime Controller 运行周期控制器,顾名思义,这是一种设置运行时间的控制器,它的效果就是使该控制器下的子项运行时间为[Runtime]中的数值(单位:s). Runt ...

  10. python 列表、字典多排序问题

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/justin051/article/det ...