bst

学习数据结构的第四天

a 夏天 提交于 2020-03-31 10:28:43
class Solution { private class BST<E extends Comparable<E>> { //这里也是暗含乾坤,必须extends呀 private Node root; private int size; private class Node { E value; Node left; Node right; public Node(E e) { value=e; left=null; right=null; } } public boolean contains(E e) { return contains(this.root,e); } private boolean contains(Node node,E e) { if(node==null) return false; if(node.value.equals(e)) //如果是引用类型的话,那么就是.equals return true; if(node.value.compareTo(e)>0) return contains(node.left,e); if(node.value.compareTo(e)<0) return contains(node.right,e); return false; } public void add(E e) { this.size++;

Leetcode:Unique Binary Search Trees

喜夏-厌秋 提交于 2020-03-24 06:16:17
Given n , how many structurally unique BST's (binary search trees) that store values 1... n ? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3分析:首先我们从分类的角度考虑,当BST的根节点分别是1,2,..n时对应的BST树的个数。当我们确定了BST的根节点k,那么以k为根节点的BST的个数等于左BST子树的个数乘以右BST子树的个数。还有一点值得注意的是对于给定的节点数BST的个数是一定的,根据这个我们可以做进一步的优化。代码如下: class Solution { public: int numTrees(int n) { if(n == 0 || n == 1) return 1; int result = 0; for(int i = 1; i <= n/2; i++) result += numTrees(i-1)*numTrees(n-i); return (n%2 == 0)?(2*result):(pow(numTrees(n/2),2) + 2*result); } }; 此外,有这句

[LeetCode] Unique Binary Search Trees

徘徊边缘 提交于 2020-03-24 06:12:31
Given n , how many structurally unique BST's (binary search trees) that store values 1... n ? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 Hide Tags Tree Dynamic Programming 思路:dp 如果集合为空,只有一种BST,即空树, UniqueTrees[0] =1 如果集合仅有一个元素,只有一种BST,即为单个节点UniqueTrees[1] = 1 UniqueTrees[2] = UniqueTrees[0] * UniqueTrees[1] (1为根的情况)+ UniqueTrees[1] * UniqueTrees[0] (2为根的情况)。 再看一遍三个元素的数组,可以发现BST的取值方式如下: UniqueTrees[3] = UniqueTrees[0]*UniqueTrees[2] (1为根的情况) + UniqueTrees[1]*UniqueTrees[1] (2为根的情况) + UniqueTrees[2]*UniqueTrees[0] (3为根的情况) 所以

Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

筅森魡賤 提交于 2020-03-24 06:07:34
Unique Binary Search Trees Given n , how many structurally unique BST's (binary search trees) that store values 1... n ? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 Unique Binary Search TreesII Given n , generate all structurally unique BST's (binary search trees) that store values 1... n . For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 分析:这个题目是要求BST有多少种排列方式,第一题是只求数量,第二题要求把结构也存储下来。本质都是深搜,第一题显然更简单 因为只要知道当前有多少个数,就可以知道有多少种排列方式了,甚至可以简单的建个存储表

[LintCode] 661. Convert BST to Greater Tree

南笙酒味 提交于 2020-03-23 04:47:03
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example Example 1: Input : {5,2,13} 5 / \ 2 13 Output : {18,20,13} 18 / \ 20 13 Example 2: Input : {5,3,15} 5 / \ 3 15 Output : {20,23,15} 20 / \ 23 15 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: the root of

[LeetCode] 450. Delete Node in a BST

寵の児 提交于 2020-03-12 08:40:33
删除二叉搜索树中的节点。题意是给一个二叉搜索树和一个节点key,请将key从BST中删除并维持BST的性质。例子, Example: root = [5,3,6,2,4,null,7] key = 3 5 / \ 3 6 / \ \ 2 4 7 Given key to delete is 3. So we find the node with value 3 and delete it. One valid answer is [5,4,6,2,null,null,7], shown in the following BST. 5 / \ 4 6 / \ 2 7 Another valid answer is [5,2,6,null,4,null,7]. 5 / \ 2 6 \ \ 4 7 因为是BST所以查找要删除的节点应该很简单,如果比root小就往左走,比root大就往右走。难点在于删除了节点之后如何维持剩下的节点依然是一个BST。分如下几种情况, 1. 如果要删除的节点没有左子树,则直接用这个节点的右孩子顶上来; 2. 如果要删除的节点没有右子树,则直接用这个节点的左孩子顶上来; 3. 如果要删除的节点有左右子树,需要去右子树里面找出最小的节点顶上来,这个节点是 右子树里面最小的左孩子 。(参见代码中的findMin函数)找到这个最小的左孩子,替换掉key之后

230. Kth Smallest Element in a BST

半城伤御伤魂 提交于 2020-03-12 06:10:45
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 Output: 3 Follow up: What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x #

leetcode 538. Convert BST to Greater Tree

淺唱寂寞╮ 提交于 2020-03-07 05:25:52
两遍遍历。第一遍是为了算出整体的和,无论什么遍历顺序都可以。第二遍必须是中序遍历,因为是为了算出比当前节点小的和。 class Solution { int sum = 0 ; int sum1 = 0 ; public TreeNode convertBST ( TreeNode root ) { plus ( root ) ; inorder ( root ) ; return root ; } public void plus ( TreeNode root ) { if ( root == null ) return ; plus ( root . left ) ; sum += root . val ; plus ( root . right ) ; } public void inorder ( TreeNode root ) { if ( root == null ) return ; inorder ( root . left ) ; sum1 += root . val ; root . val = sum - sum1 + root . val ; inorder ( root . right ) ; } } 来源: CSDN 作者: 爱打篮球的憨憨 链接: https://blog.csdn.net/xiaobailaji/article/details

MIT Introduction to Algorithms 学习笔记(六)

僤鯓⒐⒋嵵緔 提交于 2020-03-01 21:19:21
Lecture 5: Scheduling and Binary Search Trees 1. 跑道预留系统( Runway Reservation System) Airport with single (very busy) runway. “ Reservations" for future landings. When plane lands, it is removed from set of pending events . Reserve req specify "requested landing time" t. Add t to the set if no other landings are scheduled within k minutes either way. Assume that k can vary. - else error, don't schedule Example: 飞机预留的降落时间 R = (41, 46 , 49 , 56),K = 3. Goal: 系统运行在 O (lg n ) 时间内. Algorithm Key Lesson : 我们需要更快的插入方法. 2. 二叉查找树(Binary Search Trees ) 二叉查找树的每个节点(node)x都有一个key 为 key(x) .除去根(Root)外,都有双亲 p

leetcode-两数之和IV-输入BST

谁说我不能喝 提交于 2020-03-01 19:12:14
 题目来自LeetCode,链接: two-sum-iv 。具体描述为: 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。  示例1: 输入: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 输出: True  示例2: 输入: 5 / \ 3 6 / \ \ 2 4 7 Target = 28 输出: False  这跟前面的 两数之和 以及 两数之和II-输入有序数组 很相似,不过输入的是BST。  同样可以用两数之和的方法去做(直接用一个set保存target-当前数),需要遍历一次BST,每遍历一个节点就判断是否出现在set之中了,是的话返回True,否则将target-当前值加入set。  或者可以模仿有序数组的方法,先中序遍历BST得到升序数组,然后直接用双指针方法也可以解决此问题。  实际提交发现前面两种方法时间上并不是很有优势,所以想了想,其实在用前面第二种方法的时候,可以不用得到一个升序数组再用双指针的,完全可以在遍历过程中就用双指针的。为了实现这一点,可以参考栈形式的中序遍历: 当前节点初始化为root,将其所有左节点(包括左节点的左节点,一直到叶子节点为止)和所有右节点分别入栈到左节点栈和右节点栈,可以得到left为最左节点,对应最小值,right为最右节点,对应最大值