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. Zend Studio 9.0.2破解文件和注册码下载

    Zend Studio是Zend Technologies开发的PHP语言集成开发环境(IDE),是公认最好的PHP开发工具.当前Zend Studio最新版本是9.0.2. Zend Studio ...

  2. 11.【nuxt起步】-登录验证

    1.新建/pages/login.vue 2.安装cookie Cnpm install js-cookie --s 3.Login.vue增加 import Cookie from 'js-cook ...

  3. 【GLSL教程】(二)在OpenGL中使用GLSL 【转】

    http://blog.csdn.net/racehorse/article/details/6616256 设置GLSL 这一节讲述在OpenGL中配置GLSL,假设你已经写好了顶点shader和像 ...

  4. UNP学习笔记(第一章 简介)

    环境搭建 1.下载解压unpv13e.tar.gz 2.进入目录执行 ./configurecd lib //进入lib目录make //执行make命令 3.将生成的libunp.a静态库复制到/u ...

  5. 【Python】随机漫步

    创建Randomwalk()类 我们将使用Python来生成随机漫步数据,再使用matplotlib以引入瞩目的方式将这些数据呈现出来 首先创建类Randomwalk() from random im ...

  6. C语言之基本算法32—鞍点

    //数组 /* ================================================================== 题目:求随意矩阵的全部鞍点.并统计个数.(在矩阵中 ...

  7. Python 的下载安装

    学习Python牛逼的教程: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000,本文 ...

  8. eclipse中查看java源代码设置方法

    众所周知,第一次查看class文件时,eclipse会给个界面选择添加源代码路径.   但是,如果我要换源代码路径了怎么办,eclipse也不会提示了.那就使用手动的吧       方法1: 使用ec ...

  9. MySQL数据库的知识总结

    1.Mysql数据库存储引擎 概念:存储引擎其实就是如何实现存储数据,如何为存储的数据建立索引以及如何更新,查询数据等技术实现的方法.MYSQL中的数据用各种不同的技术存储在文件 (内存)中,这些技术 ...

  10. 配置fio支持rbd測试引擎

    fio的rbd測试引擎能够非常方便的对rbd进行測试.以下示范怎样安装fio支持rbd引擎. git clone git://git.kernel.dk/fio.git $ cd fio $ ./co ...