当前位置:网站首页>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;
}
};
边栏推荐
- MySQL lethal serial question 3 -- are you familiar with MySQL locks?
- Shapiro Wilk normal analysis by SPSS
- 对话吴纲:我为什么笃信“大国品牌”的崛起?
- Database dictionary Navicat automatic generation version
- Learn open62541 -- [66] UA_ Generation method of string
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- 华为应用市场应用统计数据问题大揭秘
- Open the encrypted SQLite method with sqlcipher
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- Introduction to MySQL 8 DBA foundation tutorial
猜你喜欢

简洁、快速、节约内存的Excel处理工具EasyExcel
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

Analysis of hot spots in AI technology industry

1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析

JSP webshell免杀——JSP的基础

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

Mysql database remote access permission settings

Leetcode+ 76 - 80 storm search topic

JSP webshell free -- the basis of JSP
随机推荐
Shapiro Wilk normal analysis by SPSS
[SUCTF2018]followme
What are the popular frameworks for swoole in 2022?
二叉树专题--AcWing 1589. 构建二叉搜索树
Oracle notes
使用sqlcipher打开加密的sqlite方法
快应用中实现自定义抽屉组件
【快应用】Win7系统使用华为IDE无法运行和调试项目
P1055 [NOIP2008 普及组] ISBN 号码
UWA report uses tips. Did you get it? (the fourth bullet)
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
Read H264 parameters from mediarecord recording
【TS】1368- 秒懂 TypeScript 泛型工具类型!
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
UVM learning - object attribute of UVM phase
长投学堂上面的账户安全吗?
Analysis of hot spots in AI technology industry
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
js promise. all