http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; node *insert(node *root, int key) {
if (!root) return new node(key);
if (root->data > key) root->left = insert(root->left, key);
else root->right = insert(root->right, key);
return root;
} void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} node *k_smallest(node *root, int k) {
stack<node*> S;
while () {
while (root) {
S.push(root);
root = root->left;
}
if (!S.empty()) {
root = S.top();
S.pop();
if (k- == ) return root;
k--;
root = root->right;
}
else break;
}
return NULL;
} int main() {
node *root = NULL;
root = insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
if (k_smallest(root, )) cout << k_smallest(root, )->data << endl;
else cout << "No node" << endl;
return ;
}

Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)的更多相关文章

  1. Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree

    struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...

  2. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy

    177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...

  5. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

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

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

  7. [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记

    1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...

  8. [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2

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

  9. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

随机推荐

  1. 【转】GitHub入门详细讲解

    第一:请登录https://windows.github.com/ 下载您需要的安装软件,进行安装.安装后桌面有:GitHub 和 Git Shell 第二: 申请一个帐号https://github ...

  2. 高抛低吸T+0操作要领(目前行情短线炒作的必备技能)

    最近的行情只能用操蛋来形容,但是危机中不乏机会.现在已经不是之前行情的思路,那着一个股票长线抱着,即使是好的牛股,也经不起目前行情的这 么折腾.所以,现在最适合的操作方式就是高抛低吸.今天低吸保不准明 ...

  3. IDEA下配置Spring Boot的热部署

    © 版权声明:本文为博主原创文章,转载请注明出处 devtools简介 spring-boot-devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的 ...

  4. sqlserver修改表主键自增

    alter table tname add id int  identity(1,1)

  5. 运用Arc Hydro提取河网

    Arc hydro 插件需要 spatial analyst 支持: 解决方法:Tools菜单>>Extensions...,勾选Spatial Analyst 1.设置存储路径 ApUt ...

  6. OpenGL/GLSL数据传递小记(3.x)(转)

    OpenGL/GLSL规范在不断演进着,我们渐渐走进可编程管道的时代的同时,崭新的功能接口也让我们有点缭乱的感觉.本文再次从OpenGL和GLSL之间数据的传递这一点,记录和介绍基于OpenGL3.x ...

  7. cache和内存

    CPU与内存 北桥:主桥,主要用来处理高速信号,负责与处理器的联系:CPU通过FSB前端总线来访问内存控制器. 南桥:IO桥,负责IO总线之间的通信,比如PCI总线.SATA.USB等,可以连接光驱. ...

  8. shiro集成encache

    针对多频次或者几乎不变的大数量的数据,我们可以通过缓存来实现,具体的比如说权限认证,这个,每次操作都需要权限认证,所以,这里添加encache注解.具体的认证过程是: 1,用户第一次访问用户权限信息, ...

  9. 转载:SQL 字符串操作函数

    http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 以下所有例子均Studnet表为例:  计算字符串长度len()用来 ...

  10. 粗体EditorGUI

    GUILayout.Label ("Shading", EditorStyles.boldLabel); EditorGUILayout.Space (); InspectorSu ...