bst

LeetCode 510. Inorder Successor in BST II

匿名 (未验证) 提交于 2019-12-02 23:42:01
原题链接在这里: https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree and a node in it, find the in-order successor of that node in the BST. p p.val . You will have direct access to the node but not to the root of the tree. Each node will have a reference to its parent node. Example 1: Input: root = {"$id":"1","left":{"$id":"2","left":null,"parent":{"$ref":"1"},"right":null,"val":1},"parent":null,"right":{"$id":"3","left":null,"parent":{"$ref":"1"},"right":null,"val":3},"val":2} p = 1 Output: 2 Explanation: 1's in-order successor node is 2. Note that both p and the

96. Unique Binary Search Trees

和自甴很熟 提交于 2019-12-02 08:30:31
Given n , how many structurally unique BST's (binary search trees) that store values 1 ... n ? Example: Input: 3 Output: 5 Explanation: 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 Accepted 227,417 Submissions 471,523 计算BST的核心思想就是左子树的排列个数 x 右子树的排列个数; 那么左右子树有多少种配列方式呢, 很明显这里用递归或者dp都是可以的. class Solution { public: int numTrees(int n) { vector<int> dp(n+1); //n+1是为了下面计算方便,不用n-1 dp[0]=dp[1]=1; //dp[n]代表从1到n,n个 数可以组成的不同的BST个数 for(int i=2;i<=n;++i) //从2开始计算; i代表root节点的值, for(int j=1;j<=i;++j) //j-1代表左子树的个数, i-j代表右子树的个数; 那为什么是j-1呢,因为左子树的最小个数是0

1043 Is It a Binary Search Tree (25 分)

给你一囗甜甜゛ 提交于 2019-12-02 01:58:13
1043 Is It a Binary Search Tree (25 分) 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 than or equal to 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

Codeforces 1237E Perfect Balanced Binary Search Tree

最后都变了- 提交于 2019-12-01 17:32:35
Observations 我们考虑权值是 1 到 $n$ 的 BST,它具有下列性质: 若 $k$ 是一个非根叶子,且是个左儿子,则 $k$ 的父亲是 $k+1$ 。 证明:假设 $k$ 的父亲是 $p$ 且 $p \ne k + 1$,则 $p > k + 1$;显然 $k + 1$ 不可能是 $k$ 的祖先。 设 $k$ 和 $k + 1$ 的最近公共祖先是 $t$,则有 $k < t < k + 1$ 或者 $ k + 1 < t < k$,矛盾! 同理可证,若 $k$ 是一个非根的叶子,且是个右儿子,则 $k$ 的父亲是 $k - 1$ 。 注:上述性质也可以从「BST 的任意子树中 key 都是连续的」这个性质推出。 从而可以得出,striped BST 的所有叶子都是左儿子。 perfectly balanced BST 只有最后一层可能不满,其他层都是满的。 $n$ 个点的 perfectly balanced BST 的高度是 $\floor{\log n}$ 。 Q:是否只要满足 所有叶子节点都是左儿子 除了最后一层,每层都是满的 就一定存在一种填充权值的方案使得这棵树是一棵 perfectly balanced striped BST? A:不是。 递推 有根树具有天然的递归结构。 容易看出, $n$ 个点的 perfectly balanced

AVL平衡二叉查找树

醉酒当歌 提交于 2019-12-01 16:53:59
二叉排序树: 定义 二叉排序树,又叫二叉查找树,它或者是一棵空树;或者是具有以下性质的二叉树: 1. 若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值; 2. 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值; 3. 它的左右子树也分别为二叉排序树。 比如下图就是一棵普通的二叉排序树: 如果按照中序遍历的顺序,一棵二叉排序树的输出结果就刚好是按照从小到大的顺序输出的,可以运用于二分算法。 先对其数据结构进行定义: typedef struct Binary_Tree_node { int data; //数据域 struct Binary_Tree_node* lchild, * rchild; //左右孩子结点 }Binode, * BiTree; 然后是插入操作: //假设没有相等的data,这里我们不考虑相等的数据 //插入结点 void Insert_Binary_Tree(BiTree& bst ,int t) //bst是根节点 { if (bst == NULL) //空树或者递归到了叶结点 { BiTree newp = new Binode; newp->data = t; newp->lchild = NULL; newp->rchild = NULL; bst = newp; } else { if (t > bst->data) /

04-树7 二叉搜索树的操作集 (30 分)

一笑奈何 提交于 2019-12-01 10:23:50
本题要求实现给定二叉搜索树的5种常用操作。 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( BinTree BST ); 其中 BinTree 结构定义如下: typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; 函数 Insert 将 X 插入二叉搜索树 BST 并返回结果树的根结点指针; 函数 Delete 将 X 从二叉搜索树 BST 中删除,并返回结果树的根结点指针;如果 X 不在树中,则打印一行 Not Found 并返回原树的根结点指针; 函数 Find 在二叉搜索树 BST 中找到 X ,返回该结点的指针;如果找不到则返回空指针; 函数 FindMin 返回二叉搜索树 BST 中最小元结点的指针; 函数 FindMax 返回二叉搜索树 BST

入门平衡树: Treap

醉酒当歌 提交于 2019-12-01 09:43:39
入门平衡树: \(treap\) 前言: 如有任何错误和其他问题,请联系我 微信/QQ同号:615863087 前置知识: 二叉树基础知识,即简单的图论知识。 初识 \(BST\) : \(BST\) 是 \((Binary\:\:Search\:\:Tree)\) 的简写,中文名二叉搜索树。 想要了解平衡树,我们就先要了解这样一个基础的数据结构: 二叉搜索树。 所以接下来会先大篇幅讨论 \(BST\) 了解了 \(BST\) 后, \(Treap\) 也就顺理成章了。 二叉树有两类非常重要的性质: 1:堆性质 堆性质又分为大根堆性质和小根堆性质。小根堆的根节点小于左右孩子,且这是一个递归定义。对于大根堆则是大于。 \(c++\) 提供的 \(priorioty\_queue\) 就是一个大根堆。 2: \(BST\) 性质 给定一棵二叉树,树上的每个节点都带有一个数值,成为节点的“关键吗”。对于树中任意一个节点满足 \(BST\) 性质,指: 该节点的关键码不小于他左子树的任意节点的关键码。 该节点的关键码不大于他右子树的任意节点的关键码。 举个拓展的例子,笛卡尔树既满足堆性质,又满足 \(BST\) 性质。 笛卡尔树并不常见,博主也只见过几次,也偶尔成功地用别的数据结构 \(AC\) 掉对应的题目,是下来后看题解才发现可以用笛卡尔树写。 似乎可以用单调栈替代

树的总结(遍历,BST,AVL原型,练习题)

天涯浪子 提交于 2019-12-01 09:00:53
目录 树 一.抽象数据类型 二、二叉树的性质 三、活用树的遍历 四、BST树 五、AVL树 六、BST树和AVL树练习 树 一.抽象数据类型 1.顺序存储 使用数组存储 父亲索引为 n 左孩子 2*n 右孩子 2*n+1 2.链式存储 typedef struct TNode *Position; typedef Position BinTree; /* 二叉树类型 */ struct TNode{ /* 树结点定义 */ ElementType Data; /* 结点数据 */ BinTree Left; /* 指向左子树 */ BinTree Right; /* 指向右子树 */ }; 二、二叉树的性质 1.二叉树第i层最大结点数为:2^(i-1),i>=1 2.深度为k的二叉树最大结点总数为:2^k-1,k>=1 3.对任何非空二叉树T,若n0表示叶子结点个数、n2是度为2的非叶子结点个数,那么二者满足关系n0=n2+1 void PreOrderTraversal(BinTree BT){ if(BT){ printf("%d",BT->Data); PreOrderTraversal(BT->Left); PreOrderTraversal(BT->Right); } } 中序后序同理,把打印放在中间和后面,这里不加以赘述。 3.2.非递归 以链式中序为例 void

96. Unique Binary Search Trees

允我心安 提交于 2019-11-30 15:20:54
Given n , how many structurally unique BST's (binary search trees) that store values 1 ... n ? Example: Input: 3 Output: 5 Explanation: 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 class Solution { public int numTrees(int n) { int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for(int i = 2; i <= n; i++){ for(int j = 1; j <= i; j++){ dp[i] += dp[j-1] * dp[i - j]; } } return dp[n]; } } 自底向上的DP。递推公式下图可以理解,因为F(i, n)表示n个数字且以第i个为根的bst个数,所以两个循环都是小于等于。 求和可从下图理解,本质上是因为,每个数都有资格作为root,所以答案当然要把他们加起来。 来源: https://www.cnblogs.com/wentiliangkaihua/p

653. 两数之和 IV - 输入 BST

爱⌒轻易说出口 提交于 2019-11-30 05:46:39
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def findTarget(self, root, k): """ :type root: TreeNode :type k: int :rtype: bool """ nums=[] if root==None: return None else: self.helper(root,nums) l=0 r=len(nums)-1 while l!=r: if nums[r]+nums[l]==k: return l,r elif nums[r]+nums[l]>k: r-=1 else: l+=1 return None pass def helper(self,root,nums): if root.left!=None: self.helper(root.left,nums) nums.append(root.val) if root.right!=None: self.helper(root.right,nums) 来源: https:/