4.6 Write an algorithm to find the'next'node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.

这道题实际上考察的是一种叫线索二叉树的数据结构,而构造这种树的方法称之为Morris遍历法,在我之前的博客Binary Tree Inorder Traversal 二叉树的中序遍历有详细的介绍,然而并没什么卵用,因为这道题给了个条件,说每个节点都可以链接到其父节点,这样一来就大大的简化了问题。首先我们知道二叉搜索树的性质的左<=根<右,那么其中序遍历就是一个递增的有序数列,那么我们如何找到一个节点的下一个节点呢。思路是这样的,首先我们判断输入节点是否为空,若为空直接返回NULL,若不为空,在看其右子节点是否存在,存在的话找到右子树中最左的左子节点返回。如果右子节点不存在,则说明该点的子树全遍历完了,就要往其父节点上去找未被遍历完全的节点,参见代码如下:

class Solution {
public:
TreeNode* inorderSucc(TreeNode *n) {
if (!n) return NULL;
if (n->right) {
TreeNode *p = n->right;
while (p->left) p = p->left;
return p;
} else {
TreeNode *q = n, *x = q->parent;
while (x && x->left != q) {
q = x;
x = x->parent;
}
return x;
}
}
};

[CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点的更多相关文章

  1. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  2. [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  3. 450 Delete Node in a BST 删除二叉搜索树中的结点

    详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...

  4. [Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  5. [CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树

    4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to crea ...

  6. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  7. [leetcode]450. Delete Node in a BST二叉搜索树删除节点

    二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...

  8. Second largest node in the BST

    Find the second largest node in the BST 分析: 如果root有右节点,很明显第二大的node有可能在右子树里.唯一不满足的条件就是右子树只有一个node. 这个 ...

  9. [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)

    问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...

随机推荐

  1. IE无法正常打开QC的解决方案

    方案一: 用兼容视图方式打开.(亲测IE10 可行) 方案二:(使用版本IE6-IE10) 1.安装过程中Jboss服务键入windows系统用户名密码域时总是提示用户名密码不正确! 解决方法:我的电 ...

  2. linux路由表命令

    转自此大神http://www.cnblogs.com/gunl/archive/2010/09/14/1826234.html 留在好查阅 linux 路由表维护 查看 Linux 内核路由表 使用 ...

  3. 数据仓库建模与ETL实践技巧

    数据分析系统的总体架构分为四个部分 —— 源系统.数据仓库.多维数据库.客户端(图一:pic1.bmp) 其中,数据仓库(DW)起到了数据大集中的作用.通过数据抽取,把数据从源系统源源不断地抽取出来, ...

  4. JS中的event 对象详解

    JS中的event 对象详解   JS的event对象 Event属性和方法:1. type:事件的类型,如onlick中的click:2. srcElement/target:事件源,就是发生事件的 ...

  5. C/S架构程序多种类服务器之间实现单点登录(转)

    (一) 在项目开发的过程中,经常会出现这样的情况:我们的产品包括很多,以QQ举例,如登陆.好友下载.群下载.网络硬盘.QQ游戏.QQ音乐等,总不能要求用户每次输入用户名.密码吧,为解决这个问题,高手提 ...

  6. mysql ---复制表结构---创建新表

    1.复制表结构及数据到新表CREATE TABLE 新表SELECT * FROM 旧表这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来删 ...

  7. VMware 12Pro 安装MACOS 10.10

    前言 最近帮人MacBook PRO重新安装了下10.10,在加上用了IP6,对苹果系统很有好感,所以想自己装个mac系统玩一下.虽然有了surface pro3 但是看了时间久了厌了,好想买个MAC ...

  8. 如何用js实现截取一个字符串中的数字

    比如var v ="我要提问1098";var v="我0要提问"var v="我还是要提问987"等我想要里边的 1098 ,0, 987 ...

  9. SQOOP Load Data from Oracle to Hive Table

    sqoop import -D oraoop.disabled=true \ --connect "jdbc:oracle:thin:@(description=(address=(prot ...

  10. Programming ActionScript 3.0 for Flash

    http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ec ...