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) { }
node(int v) : val(v), left(NULL), right(NULL) { }
};
node* insert(int n, node *root) {
if (root == NULL) {
root = new node(n);
return root;
}
if (n < root->val) {
if (root->left == NULL) {
root->left = new node(n);
return root->left;
}
else return insert(n, root->left);
}
if (n > root->val) {
if (root->right == NULL) {
root->right = new node(n);
return root->right;
}
else return insert(n, root->right);
}
}
//Variation: How would you find the sucessor of a node?
node* leftmost(node *n) {
if (n == NULL) return NULL;
while (n->left) n = n->left;
return n;
}
node* findSuccessor(node* n) {
if (n == NULL) return NULL;
if (!n->parent || n->right) return leftmost(n->right);
else {
while (n->parent && n->parent->right == n) n = n->parent;
return n->parent;
}
}
下面是没有parent的代码
node *minvalue(node *root) {
while (root->left) root = root->left;
return root;
}
node* successor(node *root, node *n) {
if (n->right) return minvalue(n->right);
node *succ = NULL;
while (root) {
if (n->data < root->data) {
succ = root;
root = root->left;
}
else if (n->data > root->data) root = root->right;
else break;
}
return succ;
}
Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree的更多相关文章
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...
- [Lintcode]Inorder Successor in Binary Search Tree(DFS)
题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...
- Binary Search Tree In-Order Traversal Iterative Solution
Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- Object-C 类
Classes 类 像其它的面向对象的语言一样,Object-C也提供创建对象的蓝本.即类. 首先我们在类中定义一些能够反复使用的属性和方法. 然后,我们实例化类,即对象,之后就能够使用属性和訪问. ...
- 转: JDK包含的基本组件
JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK.JDK 是整个Java的 ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- RMQ(区间求最值)
1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A.回答若干询问RMQ(A,i,j)(i,j<=n).返回数列A ...
- UE把环境变量Path改了
为了比较个文件,装了UE. 文件比较完了,环境变量也被改了. 改还不是写添加式的改,是写覆盖式的改. 搞得ant都起不动了,一看Path被改的那样(C:\hy\soft\ultraedit\Ultra ...
- sql执行顺序图
http://www.16aspx.com/cmsimages/20130325/664845013.png
- uboot移植rtc
uboot中可能会有需求提供rtc的支持目的达到uboot启动也能够进行墙上时间的显示和后面推断.大部分rtc支持的一个必要条件就是已经有i2c的支持.由于非常多的rtc是i2c接口控制的.uboot ...
- iOS 一些struct类型的NSLog输出
我们经常会输出一些坐标尺寸信息之类的,比如view的frame,是CGRect类型的,用frame.oringial.x 和frame.size.width来做NSLog参数好麻烦,还好苹果对这些常用 ...
- SPA路由机制详解(看不懂不要钱~~)
前言 总所周知,随着前端应用的业务功能起来越复杂,用户对于使用体验的要求越来越高,单面(SPA)成为前端应用的主流形式.而大型单页应用最显著特点之一就是采用的前端路由跳转子页面系统,通过改变页面的UR ...
- Linux内核源码分析方法_转
Linux内核源码分析方法 转自:http://www.cnblogs.com/fanzhidongyzby/archive/2013/03/20/2970624.html 一.内核源码之我见 Lin ...