[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; } ...
随机推荐
- 多线程基础(五)NSThread线程通信
5.多线程基础 线程间通信 什么叫线程间通信 在一个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另一个线程 在1个线程中执行完特定任务后, ...
- 使用SoapUI做webservice的模拟系统实例
实现步骤: 1.打开SoapUI工具,导入工程文件 2.右键生成一个模拟服务 3.由于要考虑到不同的输入返回不同的输出,因此需要写脚本对输入报文进行解释,然后针对输入来判断返回 3.1.建立不同的返回 ...
- 叶金荣:MySQL通用优化技巧
转自:http://mp.weixin.qq.com/s?__biz=MjM5NDE0MjI4MA==&mid=208777870&idx=1&sn=6efddd6283e4d ...
- Java GC 面试问题
转自:http://icyfenix.iteye.com/blog/715301 这个帖子的背景是今晚看到je上这张贴:http://www.iteye.com/topic/715256,心血来潮写下 ...
- 04_最长上升子序列问题(LIS)
来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题6: 问题描述:给定n个整数a1,a2,...,an,按从左到右的顺序选出尽量多的整数,组成一个上升子序列(子序列可以理解为:删除 ...
- 解决ubuntu sudo not found command的问题
将/etc/sudoers 中Defaults env_reset改成Defaults !env_reset取消掉对PATH变量的重置, 然后在/etc/bash.bashrc中最后添加alias s ...
- hdu 2048 神、上帝以及老天爷(错排)
神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Linux学习之三——操作档案与目录
一. 目录文档操作指令 1. pwd 显示目前所在目录 如果加上-P 的选项,则取得正确的目录名称,而不是以链接文件的路径来显示. 例如CentOS下,刚刚好/var/mail是/var/spool/ ...
- ubuntu 添加启动器
终于搞定了安卓开发环境,不知道折腾了多少次,多少个IDE,解决了一个问题,又冒出一个问题.烦死了,最后关头,都快放弃了,重启电脑,打开 android stuio 编译运行居然陈宫了,没有报错,why ...
- Combine small files to Sequence file
Combine small files to sequence file or avro files are a good method to feed hadoop. Small files in ...