当前位置:网站首页>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;
}
};
边栏推荐
- 洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
- 13. Semaphore critical zone protection
- MySQL lethal serial question 4 -- are you familiar with MySQL logs?
- MySQL lethal serial question 3 -- are you familiar with MySQL locks?
- Open the encrypted SQLite method with sqlcipher
- 【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
- 【AI应用】海康威视iVMS-4200软件安装
- 华为快应用中如何实现同时传递事件对象和自定义参数
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- 2.hacking-lab脚本关[详细writeup]
猜你喜欢

Introduction to MySQL 8 DBA foundation tutorial

Mysql database remote access permission settings

集成学习概览

二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)

二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)

Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application

Ks009 implement pet management system based on SSH

首份中国企业敏捷实践白皮书发布| 附完整下载

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

LabVIEW为什么浮点数会丢失精度
随机推荐
14. Code implementation of semaphore
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
(五)APA场景搭建之挡位控制设置
Easyexcel, a concise, fast and memory saving excel processing tool
PCL 从一个点云中提取一个子集
大华设备播放过程中设置播放速度
全网显示 IP 归属地,是怎么实现的?
JS settimeout() and interview questions
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
SUS系统可用性量表
【快应用】Win7系统使用华为IDE无法运行和调试项目
从MediaRecord录像中读取H264参数
How to get the password of cpolar?
JSP webshell free -- the basis of JSP
Oracle 笔记
[SUCTF2018]followme
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
JVM之垃圾回收器
Open the encrypted SQLite method with sqlcipher
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS