当前位置:网站首页>Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
2022-07-02 10:58:00 【Morgannr】
The question :
Find the successor of a given node in a binary tree .
Time complexity :
( simulation ) O(h)
Ideas :
Just discuss the situation , As shown in the figure below :
- If the current node has a right son , Then the leftmost node in the right subtree is the successor of the current node . such as F The successor is H;
- If the current node has no right son , You need to follow father The domain keeps looking up , The first one to find is its father Left son's node , Of this node father It is the successor of the current node . For example, the current node is D, Then the first satisfaction is its father The node of the left son is C, be C Of father Namely D In the subsequent , namely F yes D In the subsequent .
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode *father;
* TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {}
* };
*/
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* p) {
if (p->right)
{
p = p->right;
while (p->left)
{
p = p->left;
}
return p;
}
while (p->father && p == p->father->right) p = p->father;
return p->father;
}
};
边栏推荐
- 2022爱分析· 国央企数字化厂商全景报告
- Overview of integrated learning
- Easyexcel, a concise, fast and memory saving excel processing tool
- What are the popular frameworks for swoole in 2022?
- 如何使用IDE自动签名调试鸿蒙应用
- Read H264 parameters from mediarecord recording
- 大华设备播放过程中设置播放速度
- Oracle 笔记
- 全网显示 IP 归属地,是怎么实现的?
- The URL in the RTSP setup header of the axis device cannot take a parameter
猜你喜欢
Ks009 implement pet management system based on SSH
The URL in the RTSP setup header of the axis device cannot take a parameter
13.信号量临界区保护
JSP webshell免殺——JSP的基礎
2022-06-17
(五)APA场景搭建之挡位控制设置
12.进程同步与信号量
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
Win11 arm系统配置.net core环境变量
Open the encrypted SQLite method with sqlcipher
随机推荐
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
Kustomize user manual
PCL之K-d树与八叉树
Kustomize使用手册
Hdu1228 a + B (map mapping)
【AppLinking实战案例】通过AppLinking分享应用内图片
JS settimeout() and interview questions
nodejs+express+mysql简单博客搭建
PCL extracts a subset from a point cloud
Disassembling Meitu SaaS: driving the plane to change the engine
Is the account above changtou school safe?
Set the playback speed during the playback of UOB equipment
2. Hacking lab script off [detailed writeup]
HDU1228 A + B(map映射)
C#中索引器
PCL Eigen介绍及简单使用
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
P1055 [noip2008 popularization group] ISBN number
主键策略问题
Filtering of PCL