[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; } ...
随机推荐
- Monyer's Game 11~15关过关方法
到目前为止,玩这个小游戏并过关的人数已经达到了81人,首先Monyer要感谢各位的捧场与支持.继续上次的<Monyer's Game 6~10关过关方法>,我们来看剩下几关的过关方法. 但 ...
- 安全初始化MySql服务器
我们在安装完MySql服务器,设置好MySql的root用户密码后,就直接开始使用了,其实这样的MySql服务器还存在着一些不安全因素, 本篇演示一下用命令mysql_secure_installat ...
- OOD沉思录 --- 类和对象的关系 --- 包含关系3
4.7 类包含的对象数目不应当超过开发者短期记忆数量,这个数目通常应该是6左右 4.8 让系统在窄而深的包含体系中垂直分布 假设有如下两份菜单: 正餐 --->甜瓜 --->牛排 ---& ...
- android中的事件传递和处理机制
一直以来,都被android中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
- 关于Math类的round、floor、ceil三个方法
一.Math类这三个方法的简介 1.round():取最接近的值. 对于这个方法,查看源代码,其实现如下: public static long round(double a) { if (a != ...
- PowerDesigner概述(系统分析与建模)以及如何用PowerDesigner快速的创建出这个数据库
PowerDesigner是Sybase公司推出的一个集成了企业架构,UML(统一建模语言)和数据库的CASE(计算机辅助软件工程)工具.它 不仅可以用于系统设计和开发的不同阶段(即业务分析,概念模型 ...
- 一个fork的面试题
前两天有人问了个关于Unix的fork()系统调用的面试题,这个题正好是我大约十年前找工作时某公司问我的一个题,我觉得比较有趣,写篇文章与大家分享一下.这个题是这样的: 题目:请问下面的程序一共输出多 ...
- Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 学完STM32开发板,就选4412开发板让你有目标的学习嵌入式开发
600余页用户使用手册 linux实验手册(资料不断更新)100期配套零基础高清视频教程 轻松入门 (资料不断更新)2000人售后认证群 在线支持 售后无忧 源码全开源 原厂技术资料经典学习书籍推荐 ...
- Kafka原理与java simple producer示例
brokers和消费者使用zk来获取状态信息和追踪消息坐标. 每一个partition是一个有序的,不可变的消息序列. 只有当partition里面的file置换到磁盘文件以后,才开放给消费者来消费. ...