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. HTML5 WebAudioAPI-实例(二)

    简单播放实例1: var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { alert('您的浏览器不支持AudioContex ...

  2. java 引用资源-ClassLoader.getResource()方法

    如图,eclipse中我的包结构为:,我在 spt.app.MainFrame 中可以通过一下代码段使用资源: public static Object obj = ImageIconProxy.cl ...

  3. Express难点解析

    app.js 应用程序入口文件1.// view engine setup 设置视图引擎app.set('views', path.join(__dirname, 'views'));//告诉expr ...

  4. 浅谈angular框架

    最近新接触了一个js框架angular,这个框架有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入,以上这些全部都是属于angular特性,虽然说它的功能十分的强大 ...

  5. asp.net 网站和asp.net Web 应用程序的一处不同

    环境为:VS2008Team+.net3.5 asp.net 网站前台页面<%= %>这样绑定可以,asp.net Web 应用程序就不可以 示例代码如下: 1.asp.net网站 < ...

  6. 【POJ1195】【二维树状数组】Mobile phones

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  7. 『重构--改善既有代码的设计』读书笔记----Extract Class

    在面向对象中,对于类这个概念我们应该有一个清晰的责任认识,就是每个类应该只有一个变化点,每个类的变化应该只受到单一的因素,即每个类应该只有一个明确的责任.当然了,说时容易做时难,很多人可能都会和我一样 ...

  8. [HttpClient]传递参数

    package com.jerry.httpclient; import java.io.UnsupportedEncodingException; import java.util.ArrayLis ...

  9. 多选select实现左右添加删除

    案例:实现效果 1.选择监控城市,车辆列表显示对应城市所有车辆 2.从左边选择车辆  单击  >>   实现右侧显示添加车辆 ,左侧对应移除已选择车辆 3.右侧选中车辆     单击 &l ...

  10. Python Tutorial 学习(九)--Classes

    ## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...