bst

230. Kth Smallest Element in a BST

喜你入骨 提交于 2020-01-05 08:27:55
不要被题目迷惑,肯定是要O(N)的时间,在不改变数据结构的情况下。 就是in-order traverse,因为是BST,所以遍历顺序是从小到大,遍历的第K个元素就是第K小的。 可以Iteratively,也开始recursively. public class Solution { /* recursion version int count = 0; int res = 0; public int kthSmallest(TreeNode root, int k) { helper(root,k); return res; } public void helper(TreeNode root, int k) { if(root == null || count == k) return; helper(root.left,k); count++; if(count == k) { res = root.val; return; } helper(root.right,k); } */ public int kthSmallest(TreeNode root, int k) { int res = 0; int count = 0; Stack<TreeNode> stk = new Stack<TreeNode>(); TreeNode temp = root; while

230. Kth Smallest Element in a BST

…衆ロ難τιáo~ 提交于 2020-01-05 08:27:40
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. 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? Hint: Try to utilize the property of a BST. What if you could modify the BST node's structure? The optimal runtime complexity is O(height of BST). Similar: Binary Tree Inorder Traversal 1 Solution 1. Binary Search 2 Time Complexity: T(n) = n/2 + T(n/2) = O(n) 3 /

(栈,BST) leetcode 224. Basic Calculator 173. Binary Search Tree Iterator

旧巷老猫 提交于 2020-01-02 19:43:52
开两个栈,一个记录数字,一个记录操作符。 然后从前往后扫描整个表达式: 1. 如果遇到 (、+、-,直接入栈; 2. 如果遇到数字,则判断操作符栈的栈顶元素,如果不是 (,则弹出操作符的栈顶元素,并用相应操作更新数字栈的栈顶元素。从而保证操作符栈的栈顶最多有一个连续的+或-; 3. 如果遇到 ),此时操作符栈顶一定是 (,将其弹出。然后根据新栈顶的操作符,对数字栈顶的两个元素进行相应操作; 时间复杂度分析:每个数字和操作进栈出栈一次,所以总时间复杂度是 O(n) class Solution { public: void calc(stack<char>& op, stack<int>& num){ int y = num.top(); num.pop(); int x = num.top(); num.pop(); if(op.top() == '+') num.push(x+y); else num.push(x-y); //这里一定是先进栈的x减去后进栈的y op.pop(); } int calculate(string s) { //s 中包含 ( ) + - 非负整数 空格 // two stacks: one records numbers, one records operations stack<int> num; stack<char> op; for(int

PAT A1043. Is It a Binary Search Tree (25) [⼆叉查找树BST]

两盒软妹~` 提交于 2020-01-01 12:49:38
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The lef 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 lef and right subtrees must also be binary search trees. If we swap the lef 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 of

PTA6-12 二叉搜索树的操作集 (30分)

情到浓时终转凉″ 提交于 2019-12-23 02:08:20
本题要求实现给定二叉搜索树的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 中最小元结点的指针; 函数

[LC] 230. Kth Smallest Element in a BST

安稳与你 提交于 2019-12-22 14:02:48
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 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 int res; 12 int count; 13 public int kthSmallest(TreeNode root, int k) { 14 res =

LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)

瘦欲@ 提交于 2019-12-18 15:14:51
题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有如下特征: 节点的左子树只包含 小于 当前节点的数。 节点的右子树只包含 大于 当前节点的数。 所有左子树和右子树自身必须也是二叉搜索树。 示例 1: 输入: 2 / \ 1 3 输出: true 示例 2: 输入: 5 / \ 1 4 / \ 3 6 输出: false 解释: 输入为: [5,1,4,null,null,3,6]。 根节点的值为 5 ,但是其右子节点值为 4 。 解题思路 用树的后序遍历思想,对于每次遍历到的根节点,根据左右子树是否为BST分为四种情况: 若左右子树都不为空,则分别递归判断左右子树是否为BST,并且分别传回左右子树的最小值和最大值。若左右子树都为BST,则比较左子树最大值是否小于根结点值以及右子树最小值是否小于根结点值,满足则为BST并记录本子树的最小值与最大值,否则整棵树不是BST 若左子树为空右子树不为空,则判断右子树是否为BST, 并且传回右子树的最小值和最大值。若右子树为BST,则比较右子树 最小值是否小于根结点值,满足则为BST并记录本子树的最小值与最大值,否则整棵树不是BST 若右子树为空左子树不为空,则判断左子树是否为BST, 并且传回左子树的最小值和最大值。若左子树为BST,则比较左子树 最小值是否小于根结点值

数据结构-二叉搜索树(BST binary search tree)

醉酒当歌 提交于 2019-12-18 05:51:32
本文由 @呆代待殆 原创,转载请注明出处: http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来表示,每个节点除了key和卫星数据(除了二叉树节点的基本数据以外人为添加的数据,这些数据和树的基本结构无关),还有left、right、parent,分别指向节点的左孩子、右孩子和父节点,如果对应的节点不存在则指向NIL节点(因为最简单的二叉搜索树中的NIL节点里并没有有用的信息,所以在实现的时候简单的指向null也可以,本文的代码部分就会这么处理)。 二叉搜索树的性质 1,任意节点x,其左子树中的key不大于x.key,其右子树中的key不小于x.key。 2,不同的二叉搜索树可以代表同一组值的集合。 3,二叉搜索树的基本操作和树的高度成正比,所以如果是一棵 完全二叉树 的话最坏运行时间为 Θ(lg n ) ,但是若是一个n个节点连接成的 线性树 ,那么最坏运行时间是 Θ( n ) 。 4,根节点是唯一一个parent指针指向NIL节点的节点。 5,每一个节点至少包括key、left、right与parent四个属性,构建二叉搜索树时,必须存在针对key的比较算法。 下面给出一张wiki百科上的二叉搜索树的图示 二叉搜索树的操作 二叉搜索树的基本结构(C++) 1 //节点结构

BST树(双重指针版,未完)

谁说胖子不能爱 提交于 2019-12-14 02:38:09
这个双重指针还是不太明白。 不用引用操作符的话,就只能用双重指针来操作了。 然后要记住:在main函数外要修改谁就要传谁的地址。 //在传参的函数中,想要修改谁,就要传谁的地址。 #include<cstdio> #include<algorithm> #include<iostream> #include<cstdlib> using namespace std; typedef struct node { int data; //二重指针用来一重指针的值。 node **l; node **r; node *ll; node *rr; }BST; bool Insert(BST **bt,int key) { if(*bt==NULL) { *bt=(BST*)malloc(sizeof(BST)); (*bt)->data=key; (*bt)->ll=NULL; (*bt)->l=&((*bt)->ll); (*bt)->rr=NULL; (*bt)->r=&((*bt)->rr); return true; } else if(key==(*bt)->data) return false; else if(key<(*bt)->data) return Insert((*bt)->l,key); else return Insert((*bt)->r,key); }

Splay

ⅰ亾dé卋堺 提交于 2019-12-14 02:37:17
本文参考资料: From yyb: Link 正文:关于SPLAY 其实我更偏向于把splay叫做 cosplay 讲平衡树总逃不过BST(Binary Search Tree),二叉搜索树,以下是BST的性质: 一棵合法的BST每个节点上都带有一个数值,我们将其称为节点的“关键码”。那么对于一棵BST上的任意节点,满足: 该节点的关键码不小于它左子树的任意结点的关键码 该结点的关键码不大于它右子树的任意结点的关键码 显然 ,BST的中序遍历是一个递增的序列 建立一棵BST 因为笔者很懒,不想到处判边界,所以我们一般可以在一棵空的BST中预先插入两个结点,一个正无穷,一个负无穷,如图: const int SIZE = 1e5 + 5 ; const int INF = 0x7fffffff ; struct BSTNode { int l , r ; // 左右儿子的编号 int val ; // 关键码 } T [ SIZE ] ; int tot , root ; int clone ( int val ) { // 新建节点 T [ ++ tot ] . val = val ; return tot ; } void build ( ) { clone ( - INF ) , clone ( INF ) ; root = 1 , T [ 1 ] . r = 2 ; }