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 ...
随机推荐
- ubuntu下C/C++编程起步
1. 安装VMware虚拟机软件 2. 在VMware中安装linux系统,这里安装的是Ubuntu.(用 VMware 安装 Ubuntu 12.04详细过程图解) 3. ubuntu有自带文本编辑 ...
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 1>
这段时间忙于看文献,没用过VS了. 今天用着用着就报错了: LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 1> 问度娘,大神给出了解决方法 ...
- Spark Streaming的样本demo统计
废话不多说,直接上代码 package com.demo; import java.util.List; import java.util.regex.Pattern; import org.apac ...
- TensorFlow笔记四:从生成和保存模型 -> 调用使用模型
TensorFlow常用的示例一般都是生成模型和测试模型写在一起,每次更换测试数据都要重新训练,过于麻烦, 以下采用先生成并保存本地模型,然后后续程序调用测试. 示例一:线性回归预测 make.py ...
- PowerMockito的简单的介绍
转载:http://blog.csdn.net/u012881904/article/details/51334747 我们的依赖的配置 <properties> <powermoc ...
- cart算法
- 谈 API 的撰写 - 架构
在 谈 API 的撰写 - 总览 里我们谈到了做一个 API 系统的基本思路和一些组件的选型,今天谈谈架构. 部署 首先要考虑的架构是部署的架构.部署的方案往往会深刻影响着系统的结构.我们需要问自己一 ...
- 手把手教你画AndroidK线分时图及指标
先废话一下:来到公司之前.项目是由外包公司做的,面试初,没有接触过分时图k线这块,认为好难,我能搞定不.可是一段时间之后,发现之前做的那是一片稀烂,可是这货是主功能啊.迟早的自己操刀,痛下决心,开搞, ...
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决的方法
今天要写一个联系人搜索算法. 百度了下, 在code4App中找到相关代码. 可是自己跑了下, 发现报错. 错误内容例如以下: "Cast from pointer to smaller t ...
- Cloudera
官方文档: http://www.cloudera.com/content/cloudera/en/documentation/core/latest/ 博客教程 http://www.wangyon ...