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的更多相关文章

  1. 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 ...

  2. 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree

    [抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...

  3. [Lintcode]Inorder Successor in Binary Search Tree(DFS)

    题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...

  4. Binary Search Tree In-Order Traversal Iterative Solution

    Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...

  5. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  8. 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  ...

  9. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. selenium遇到不可编辑input和隐藏input如何赋值

    js = 'document.getElementsByName("m:csr_mt_gnccspjclfbxd:bmshldID")[0].type="text&quo ...

  2. masm学习

    . .model flat,stdcall option casemap:none ; case sensitive ;http://www.popbook.com/wbbs/topic.asp?l_ ...

  3. 【2048小游戏】——原生js爬坑之封装行的移动算法&事件

    引言:2048小游戏的核心玩法是移动行,包括横行和纵行,玩家可以选择4个方向,然后所有行内的数字就会随着行的移动而向特定的方向移动.这个行的移动是一个需要重复调用的算法,所以这里就要将一行的移动算法封 ...

  4. 社区之星礼品开箱——感谢CSDN

    前言 尽管已经看过国内外无数的开箱.评測视频,也看过无数国内社区的各种玩具.电子产品.摄影的分享贴.自己却从未写过--摄影水平有限以及懒-- 昨天看到上图的文章,看到最后都说了应该晒晒照片.写写博客, ...

  5. SqlServer 垂直分表

    当单表数据太多时.我们能够水平划分,參考 SqlServer 分区视图实现水平分表 ,水平划分能够提高表的一些性能. 而 垂直分表 则相对非常少见到和用到,由于这可能是数据库设计上的问题了.假设数据库 ...

  6. CSRF Token

    本文参考自:https://blog.csdn.net/lion19930924/article/details/50955000 目的是防御CSRF攻击. Token就是令牌,最大的特点就是随机性, ...

  7. apue学习笔记(第十一章 线程)

    本章将进一步深入理解进程,了解如何使用多个控制线程(简单得说就是线程)在单进程环境中执行多个任务. 线程概念 每个线程都包含有表示执行环境所必须的信息:线程ID.一组寄存器值.栈.调度优先级和策略.信 ...

  8. eclipse 如何把java项目转成web项目

    经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 解决步骤: 1.进入项目目录,可看到.project文件,打开. 2.找到<natures> ...

  9. js:string转int

    http://blog.csdn.net/leidengyan/article/details/5503594 <script>     var   str='1250' ;   aler ...

  10. php excel文件导出之二 图像导出

    PHP文件导出 之图像 和 文字同一时候导出 事实上之前写了个php文件导出.跟这个极为相似,由于项目须要对图像进行导出.查询一番.又写了一个, 这个能实现图像的导出(仅仅能是本地图像,不能使用远程图 ...