当前位置:网站首页>Sword finger offer 57 Next node of binary tree
Sword finger offer 57 Next node of binary tree
2022-06-22 02:49:00 【SS_ zico】
Cattle guest :JZ57
Given a binary tree and one of its nodes , Please find the next node in the middle order traversal order and return . Be careful , The nodes in the tree contain not only left and right child nodes , It also contains a pointer to the parent node .
Analyze the next node of the binary tree , There are the following :
1. The binary tree is empty , It returns null ;
2. Node right child exists , Set a pointer from the right child of the node , The leaf node found along the pointer to the left child node is the next node ;
3. Node is not root . If the node is the left child of its parent node , Then return to the parent node ; Otherwise, continue to traverse the parent node of its parent node upward , Repeat the previous judgment , Return results . The code is as follows :
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode==NULL)
return NULL;
if(pNode->right!=NULL)
{
pNode=pNode->right;
while(pNode->left!=NULL)
pNode=pNode->left;
return pNode;
}
while(pNode->next!=NULL)
{
TreeLinkNode *proot=pNode->next;
if(proot->left==pNode)
return proot;
pNode=pNode->next;
}
return NULL;
}
};
边栏推荐
- Day14QProgressBar2021-10-17
- Wechat applet film and television review and exchange platform system graduation design (1) development outline
- [go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network
- fatal error: png++/png.hpp: 没有那个文件或目录
- [5. high precision subtraction]
- 如何选择合适的 Neo4j 版本(2022版)
- Graphconnect 2022 at a glance
- Game Jam开发周期
- Architecture and practice of vivo container cluster monitoring system
- Graphacademy course explanation: introduction to neo4j graph data science
猜你喜欢

【1. 快速排序】

ATM simulation system

最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened

All the knowledge you want to know about the PMP Exam is here

An article thoroughly learns to draw data flow diagrams

Day16QtQLabel2021-10-22

Graphconnect 2022 at a glance
![[6. high precision multiplication]](/img/83/1659503e62839c395ca7849596d6fc.png)
[6. high precision multiplication]

The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it
随机推荐
HarmonyOS鸿蒙使用ORM Bee访问数据库实例
C++ primer Chapter 2 summary of variables and basic types
PMP pre exam guide on June 25, you need to do these well
June25,2022 PMP Exam clearance manual-3
Microsoft Internet Explorer was permanently closed on June 15
【8、一维前缀和】
Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
Neo4j 智能供应链应用源代码简析
Fabric. JS iText set italics manually
Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
Linxu modify the permissions of the folder so that everyone can access 777
fatal error: png++/png.hpp: 没有那个文件或目录
EMC Radiation Emission rectification - principle Case Analysis
Graphacademy course explanation: Fundamentals of neo4j graph data science
PostgreSQL fetches data according to the size of the time field
Flash back when GoLand starts
GraphConnect 2022 大会的产品发布一览
从数据库的分类说起,一文了解图数据库
import和require在浏览器和node环境下的实现差异
PMP项目管理知识该如何运用?