当前位置:网站首页>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;
}
};
边栏推荐
- Hdu1228 a + B (map mapping)
- 2. Hacking lab script off [detailed writeup]
- 一招快速实现自定义快应用titlebar
- AI技术产业热点分析
- Thanos Receiver
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
- 二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
- Leetcode+ 76 - 80 storm search topic
- 使用华为性能管理服务,按需配置采样率
- PCL projection point cloud
猜你喜欢
2.hacking-lab脚本关[详细writeup]
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
14. Code implementation of semaphore
Overview of integrated learning
Use of vscode tool
Hdu1228 a + B (map mapping)
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
Shell programming 01_ Shell foundation
【AI应用】海康威视iVMS-4200软件安装
Nodejs+express+mysql simple blog building
随机推荐
[SUCTF2018]followme
Kustomize user manual
Solution of mysql8 forgetting password file in Windows Environment
Operator-1 first acquaintance with operator
Start class, data analysis, high salary training plan, elite class
【快应用】Win7系统使用华为IDE无法运行和调试项目
UWA报告使用小技巧,你get了吗?(第四弹)
《实习报告》Skywalking分布式链路追踪?
What is the significance of the college entrance examination
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
【付费推广】常见问题合集,推荐榜单FAQ
点云投影图片
从.bag文件中读取并保存.jpg图片和.pcd点云
QT学习日记7——QMainWindow
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
HDU1228 A + B(map映射)
UVM learning - build a simple UVM verification platform
4. Random variables
拆解美图SaaS:开着飞机换引擎
如何用list组件实现tabbar标题栏