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. mysql 杀掉(kill) lock进程脚本

    杀掉lock进程最快的方法是重启mysql,像你这种情况,1000多sql锁住了,最好是重启如果不允许重启,我提供一个shell脚本,生成 kill id命令杀掉lock线程,如下:--------- ...

  2. Android Zxing 加入闪光灯功能

    近期做了关于二维码解析的模块 选用的是google的开源projectZxing 在Zxing 加入闪光灯功能 例如以下: 在 com.xxx.xxx.Zxing.camera 包下的CameraMa ...

  3. 匿名函数 invoke

    delegate string MyDele(string str); string MyFun(string str) { return str; } private void Form1_Load ...

  4. Hibernate二次学习一----------搭建Hibernate

    目录 1. 项目结构 1.2 hibernate.cfg.xml 1.3 entity 1.4 entity.hbm.xml 2. 测试 3. 总结 © 版权声明:本文为博主原创文章,转载请注明出处 ...

  5. atititi.soa  微服务 区别 联系 优缺点.doc

    atititi.soa  微服务 区别 联系 优缺点.doc 1. 应用微服务的动机,跟传统巨石应用的比较1 2. 面向服务架构(SOA)  esb2 3. 微服务架构(Microservices)2 ...

  6. Nginx服务启动脚本

    #!/bin/sh # chkconfig: 2345 40 98 # description: Start/Stop Nginx server path=/application/nginx/sbi ...

  7. hashmap的equal和hashcode为什么要同时重写

    如果你重载了equals,比如说是基于对象的内容实现的,而保留hashCode的实现不变,那么很可能某两个对象明明是“相等”,而hashCode却不一样. 这样,当你用其中的一个作为键保存到hashM ...

  8. 使用SpannableString实现一个load小动画

    依然是github开源项目:WaitingDots 这个项目代码不多,实现的非常easy.可是非常有意思由于动画的基本元素不是画出来的,而是使用了spannableString来实现. DotsTex ...

  9. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: Unknown column 'viewpoint' in 'field list'

    问题描述:当我在model中添加了一下代码以后数据库报错: 添加的代码为: private Viewpoint viewpoint; public Viewpoint getViewpoint() { ...

  10. 写shader注意的一些报错

    1.Shader warning in 'Custom/1': Both vertex and fragment programs must be present in a CGPROGRAM. Ex ...