[Locked] Inorder Successor in BST
Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Given tree = [2,1] and node = 1:
2
/
1
return node 2.
Given tree = [2,1,3] and node = 2:
2
/ \
1 3
return node 3.
If the given node has no in-order successor in the tree, return null.
O(h), where h is the height of the BST.
分析:
一般情况下,目标节点的右子节点即为中序下一个节点;如果该目标节点无右子节点,则中序下一个节点为从根节点到搜索目标节点过程中最后一次做出左节点遍历的节点。
代码:
TreeNode *successor(TreeNode *root, TreeNode *p) {
TreeNode *cur = root, *record = NULL;
while(cur != p) {
if(p->val < cur->val) {
record = cur;
cur = cur->left;
}
else
cur = cur->right;
}
return cur->right ? cur->right : record;
}
[Locked] Inorder Successor in BST的更多相关文章
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- LeetCode Inorder Successor in BST
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...
- [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- Leetcode 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 本题 ...
- 285. Inorder Successor in BST
题目: Given a binary search tree and a node in it, find the in-order successor of that node in the BST ...
- Inorder Successor in BST 解答
Question Given a binary search tree and a node in it, find the in-order successor of that node in th ...
- [Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- LeetCode 510. Inorder Successor in BST II
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...
随机推荐
- 在DataTable中更新、删除数据
/*在DataTable中选择记录*/ /* 向DataTable中插入记录如上,更新和删除如下: * ----但是在更新和删除前,首先要找出要更新和删除 ...
- C#开发基于Http的LaTeX数学公式转换器
本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过TextBox获取用户输入的LaTeX数学表达式, ...
- 如何将硬盘GPT分区转换为MBR分区模式
现在新出的笔记本普遍自带WIN8系统,硬盘分区一般都采用GPT格式,但是包括WIN7及以下的系统都无法安装在GPT格式的硬盘上,因此,如果我们需要安装WIN7系统,需要将硬盘分区从GPT转换成MBR格 ...
- .net RAW(16)与GUID互相转换
.net 1.raw转guidnew guid(byte[] id);2.guid转rawGuid result;string ids = BitConverter.ToString(result.T ...
- Java jdk环境搭建
java JDK的配置在我的电脑环境变量中配置: 主要的配置参数path 例:path = C:\jdk1.7.0_13\bin ; 另外一个JAVA_HOME 例:JAVA_HOME = C:\j ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...
- 通过命令修改wampserver的mysql密码
WAMP安装好后,mysql教程密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按 ...
- Android获取相册图片
1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...
- MAC OSX使用篇
前述: 从windows平台切换到osx平台,的确没能一下子适应过来,在使用过程当中遇到了很多问题. link1:osx卸载应用程序的四种方法 link2:(推荐看)开始使用Mac OS X——写给M ...
- 应用Oracle(用户创建和授权)
使用oracle命令创建用户,并授权. 系统管理员身份登录 cmd中, sqlplus / as sysdba 若登录失败,则 sqlplus system/[数据库创建时指定密码] as sysdb ...