bst

Serialize and Deserialize BST

喜夏-厌秋 提交于 2020-02-03 02:52:34
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure. The encoded string should

BST总结

谁说胖子不能爱 提交于 2020-02-03 02:16:06
BST 是 Treap 的基础。 这位大佬写的不错 只写一点基本操作方便自己理解。 性质:左儿子 < 自己 < 右儿子 上图 插入 根据性质,比当前结点小就插入左子树,大就插入右子树,相等直接令当前结点的 cnt++ 即可。 void insert(int o, int k) {//在当前结点 o 插入 k 值 tr[o].sz++; if (tr[o].val == k) { tr[o].cnt++; return; } if (v < tr[o].val) { if (tr[o].ls != 0) insert(tr[o].ls, k); else { tot++; tr[tot].val = k, tr[tot].sz = tr[tot].cnt = 1; tr[o].ls = tot; } } else { if (tr[o].rs != 0) insert(tr[o].rs, k); else { tot++; tr[tot].val = k, tr[tot].sz = tr[tot].cnt = 1; tr[o].rs = tot; } } 删除 直接找到该数字所在的结点,令 cnt-- 就好了。 代码就不写了 因为上面那位大佬没写 前驱 一个数的前驱是指小于这个数的最大的数。 如果当前结点的值大于这个数,直接递归进左子树中找,小于的话就递归进右子树中找。 int

CF1025D Recovering BST

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 05:55:29
一、题目 点此看题 二、解法 考虑暴力做法,设 f [ l ] [ r ] [ u ] [ 0 / 1 ] f[l][r][u][0/1] f [ l ] [ r ] [ u ] [ 0 / 1 ] 表示 [ l , r ] [l,r] [ l , r ] 作为 u u u 的左儿子 / / / 右儿子是否合法,但是这种做法在 n<=700 的数据中会 MLE 。 考虑压缩无用的状态,由于我们需要构造的是一颗 二叉搜索树 ,所以一段区间 [ l , r ] [l,r] [ l , r ] 如果作为左儿子,那么根一定是 r + 1 r+1 r + 1 ,并且一段区间是否合法是可以通过枚举根节点来判断的,于是我们可以这么定义状态: L [ l , r ] L[l,r] L [ l , r ] 表示 [ l , r − 1 ] [l,r-1] [ l , r − 1 ] 作为 r r r 的左端点是否合法 R [ l , r ] R[l,r] R [ l , r ] 表示 [ l + 1 , r ] [l+1,r] [ l + 1 , r ] 作为 l l l 的右端点是否合法 然后就可以愉快的转移了,我们 左端点从大到小,右端点从小到大 ,来枚举一个区间,我们首先判断这个区间是否合法,枚举根(从 L [ l , k ] , R [ k , r ] L[l,k],R[k,r] L [ l

[leetcode]Inorder Successor in BST

你。 提交于 2020-02-01 18:15:04
简单的利用递归来做 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def inorderTraverse(self, node: 'TreeNode', p: 'TreeNode', prev: ['TreeNode'], result: ['TreeNode']) -> 'TreeNode': if not node: return if result[0]: return if node.left: self.inorderTraverse(node.left, p, prev, result) if prev[0] and prev[0].val == p.val: result[0] = node prev[0] = node if node.right: self.inorderTraverse(node.right, p, prev, result) def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode': prev =

hackerank-30days of code-BST

♀尐吖头ヾ 提交于 2020-02-01 16:15:57
应该大家都挺熟悉的, BST 的特点: 节点的左子树仅包含其键小于该节点的键的节点。 节点的右子树仅包含键大于该节点的键的节点。 左和右子树也都必须是二进制搜索树。 这道题是计算BST 的高度, 首先从root开始,然后分别计算左右孩子的高度, 然后 因为是算edge,所以就不用+1; int getHeight(Node* root){ //Write your code here if(root == nullptr) { return 0; } int leftCount = this->getHeight(root->left); if(root->left != nullptr) { leftCount ++; } int rightCount = this->getHeight(root->right); if(root->right != nullptr) { rightCount ++; } return leftCount>rightCount ? leftCount:rightCount; } 还是需要考虑root是否为null,这是recursive的终止条件, ,如果不为nullptr,就有左指针或右指针, 分别计算,知道叶子层的时候, child=0,此时返回倒数第一层的height, 自下而上, 得出倒数第二层的高度–>直到最后一个循环

红黑树算法原理

拜拜、爱过 提交于 2020-02-01 15:58:53
原文: 红黑树深入剖析及Java实现 ,本文修改了原文的一些小错误,如果想看红黑树的Java实现可以到原文去看。 红黑树是平衡二叉查找树的一种 。为了深入理解红黑树,我们需要从二叉查找树开始讲起。 BST 二叉查找树(Binary Search Tree,简称BST)是一棵二叉树,它的左子节点的值比父节点的值要小,右节点的值要比父节点的值大。 它的高度决定了它的查找效率。 在理想的情况下,二叉查找树增删查改的时间复杂度为O(logN)(其中N为节点数),最坏的情况下为O(N)。当它的高度为logN+1时,我们就说二叉查找树是平衡的。 BST的查找操作 T key = a search key Node root = point to the root of a BST while ( true ){ if (root== null ){ break ; } if (root.value.equals(key)){ return root; } else if (key.compareTo(root.value)< 0 ){ root = root.left; } else { root = root.right; } } return null ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 从程序中可以看出,当BST查找的时候

PAT_A1115#Counting Nodes in a BST

依然范特西╮ 提交于 2020-01-31 13:02:52
Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than or equal to the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree. Input

[LC] 285. Inorder Successor in BST

强颜欢笑 提交于 2020-01-30 12:19:33
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. The successor of a node p is the node with the smallest key greater than p.val . Example 1: Input: root = [2,1,3], p = 1 Output: 2 Explanation: 1's in-order successor node is 2. Note that both p and the return value is of TreeNode type. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { TreeNode res = null; while (root !=

PAT甲级-1043-Is It a Binary Search Tree(二叉搜索树+先序转后序)

爱⌒轻易说出口 提交于 2020-01-30 08:12:37
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater or equal to than the node’s key. Both the left and right subtrees must also be binary search trees. If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST. Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image

[LeetCode] 938. Range Sum of BST

徘徊边缘 提交于 2020-01-30 03:59:56
二叉搜索树的范围和。题意是给一个二叉搜索树和一个范围(L, R),请输出二叉搜索树里面所有node值介于L和R之间的node值的加和(sum)。例子, Example 1: Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32 Example 2: Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 Output: 23 两种做法,迭代和递归。 时间和空间复杂度都是O(n)。 迭代 - 中序遍历 1 /** 2 * @param {TreeNode} root 3 * @param {number} L 4 * @param {number} R 5 * @return {number} 6 */ 7 var rangeSumBST = function (root, L, R) { 8 var arr = [], sum = 0; 9 inorder(root, arr); 10 for (var i = 0; i < arr.length; i++) { 11 if (arr[i] >= L && arr[i] <= R) { 12 sum = sum + arr[i]; 13 } 14 } 15 return sum; 16 }; 17 18