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.

Example

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.

Note

If the given node has no in-order successor in the tree, return null.

Challenge

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

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

  2. LeetCode Inorder Successor in BST

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...

  3. [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 ...

  4. [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 ...

  5. 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. 本题 ...

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

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

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

  9. LeetCode 510. Inorder Successor in BST II

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...

随机推荐

  1. node 搭建开发框架express

    参考地址: http://www.itnose.net/detail/6095003.html 开发环境 E:\project> node -v v0.10.30 E:\project> ...

  2. objective-c相关知识点

    1,objective-c中实现线程同步: Mutexlock (互斥锁).NSCondition lock (条件锁)消息传送 2,UDP和TCP: TCP :传输控制协议,可以提供面向连接的.可靠 ...

  3. EBS成本核算方法

    业务背景 成本核算方法,对应EBS系统中的成本方法,有四种: 1.标准成本 2.平均成本 平均成本又分为永续平均成本,即 Average Cost 期间平均成本,按照期间(自然月)来计算的平均成本 F ...

  4. Facade 模式

    在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...

  5. SGU 230. Weighings (拓扑排序)

    题意: 给出质量为1~n的n个箱子的m对轻重关系,输出一种可能的箱子的质量排列. Solution: 拓扑排序,注意要处理重边. #include <iostream> #include ...

  6. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  7. centos7 开机启动某些程序的方法

    针对svn,nginx每次重启后均要手工启动,好麻烦,所以考虑将其做成开机启动,做成服务好麻烦,考虑像windows 一样,放在某个启动项中完成. 打开启动文件后,发现里面文件内容如下: #!/bin ...

  8. 获取IP城市

     新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...

  9. 初涉JavaScript模式 (3) : 字面量

    什么是字面量? 在编程语言中,字面量是一种表示值的记法.例如,"Hello, World!" 在许多语言中都表示一个字符串字面量(string literal ),JavaScri ...

  10. 数据库,inner join,left join right join 的区别

    假设有两个表: 学生和课程 student:              class: id    student          id       class    studentId 1      ...