[CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
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 寻找二叉搜索树中下一个节点的更多相关文章
- [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 ...
 - [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 ...
 - 450 Delete Node in a BST 删除二叉搜索树中的结点
		
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
 - [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 ...
 - [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 ...
 - [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 ...
 - [leetcode]450. Delete Node in a BST二叉搜索树删除节点
		
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
 - Second largest node in the BST
		
Find the second largest node in the BST 分析: 如果root有右节点,很明显第二大的node有可能在右子树里.唯一不满足的条件就是右子树只有一个node. 这个 ...
 - [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
		
问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
 
随机推荐
- iOS之UI--涂鸦画板实例 (有待更新)
			
首先是搭建框架 其他的略过,直接展示效果: 然后接下来上传搭建好两个控制器框架的源码百度云下载链接: http://pan.baidu.com/s/1skjpDox 密码: ardx ,工程里面还有我 ...
 - CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)
			
这篇文章主要介绍了CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)的方法,需要的朋友可以参考下 文章写的不错,很详细:IDO转载自网络: 准备篇: 1.配置防火墙,开启 ...
 - Effective Java 63 Include failure-capture information in detail message
			
Principle To capture the failure, the detail message of an exception should contain the values of al ...
 - MVC如何在单独的类库中添加区域
			
今天要做一个将区域放到单独的类库中的程序,其实就是多加几个引用的问题,但是我比较喜欢这种设计结构,因为这样的话可以把单独的应用逻辑放在单独的类库中处理,项目看起来更清晰分明,所以写了这个随笔. 首先创 ...
 - .NET三层架构例子超链接可以点击显示内容页面
			
在研究了一个星期的三层架构写出的一个小功能,使用三层架构并实现点击新闻标题可以跳转到自己写的新闻页面. 首先是一个DBHelper,这个不是我自己写的,是朋友给我的 using System; usi ...
 - 001.linux下clock()检测程序运行时间
			
#include <stdio.h> #include <time.h> int main() { int i; int k; clock_t start,end; //clo ...
 - kvm解决1000M网卡问题
			
1.当我们安装完虚拟机, 发现虚拟机竟然是 100M 网络, 传输速率很低, 那是怎么导致的呢,如何来解决呢? 需要我们修改 vm01.xml 配置文件网卡段,添加如下红色标记行,改 为 e1000, ...
 - 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
			
原文 http://blog.csdn.net/bugdemo/article/details/9083497 主题 技术 在引用WebService后,程序运行到实例化WebService时报错, ...
 - Linux查看BIOS信息
			
http://www.linuxde.net/2013/02/12499.html
 - Spring4定时器 cronTrigger和simpleTrigger实现方法
			
spring4定时器 cronTrigger和simpleTrigger实现方法 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许 ...