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. IOS设计模式-简单工厂

    为什么 “简单工厂原理” 叫简单工厂,因为这个工厂类不是抽象类,而是实体类,工厂本身就是一个具体的对象. 写一个例子演示简单工厂模式: >>>>>>>> ...

  2. winform dateTimePicker选择时间控件-选择小时、分钟、秒

    今天对公司项目进行改版(一个c/s客户端程序),要求dateTimePicker 能够选择小时,分钟.但找了很久,发现没有相关的简化控件,都是web的,没有winform的. 可是功夫不负有心人啊. ...

  3. canvas & animation

    1.using canvas using canvas to set difference property. it will showing like an animation. The true ...

  4. (传输层)TCP协议

    目录 首部格式数据单位特定注意自动重传请求ARQ具体实现发送缓存接收缓存滑动窗口确认丢失和确认迟到超时重传时间选择报文段的发送时机运输连接发送TCP请求客户端拥塞处理相关概念避免拥塞具体实现TCP 的 ...

  5. TOMCAT报错:HTTP Status 404 -

    构建struts2工程师,tomcat报错: HTTP Status 404 - type Status report message description The requested resour ...

  6. cmd常用命令 和 sql server相关基础

    在Java开发中 ms sql server 接触算是比较少的,本文记录一些ms sql server的基础知识. 1. 为表字段增加索引:create index user_openid on us ...

  7. .NET 分页

    .net分页 1.存储过程create procedure 存储过程名( @pageIndex int,                         //第几页 @pageSize int     ...

  8. ProjectManager Alpha8 - 项目管理器,管理起开发中的项目很方便

    话不多说= = 放几张图了: 文件下载: 32位下载:Package_ProjectManager-1.13.12.exe 64位下载:Package_ProjectManager_x64-1.13. ...

  9. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. JS键盘事件监听

    window.onload=function(){ var keyword = document.getElementById("keyword"); keyword.onkeyu ...