当前位置:网站首页>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;
}
};
边栏推荐
- Flash back when GoLand starts
- PMP pre exam guide on June 25, you need to do these well
- The brand, products and services are working together. What will Dongfeng Nissan do next?
- Using OKR for HR digital transformation
- JVM makes wheels
- 银联支付 返回商户 Nignx post请求405
- UnionPay payment return merchant nignx post request 405
- Write your own kubernetes controller
- Day12QFile2021-09-27
- How to select the appropriate version of neo4j (version 2022)
猜你喜欢

DAST black box vulnerability scanner part 4: scanning performance

Architecture and practice of vivo container cluster monitoring system

Pytorch visualization

Starting from the classification of database, I understand the graph database
![[4. high precision addition]](/img/8c/1d07597b5ff3a573b453ac1ca19a5c.png)
[4. high precision addition]

Select for i/0 multiplexing

Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition

Neo4j 智能供应链应用源代码简析

JS special effects in the construction of animated web pages

PMP reference related agile knowledge
随机推荐
Transformation numérique des RH avec okr
Graphconnect 2022 at a glance
2022年买理财产品买三个月还是半年?
[3. binary integer and floating point number]
EMC Radiation Emission rectification - principle Case Analysis
FPGA-Xilinx 7系列FPGA DDR3硬件设计规则
FPGA Xilinx 7 Series FPGA DDR3 hardware design rules
[8. One dimensional prefix and]
Zhixiang Jintai rushes to the scientific innovation board: the annual revenue is 39.19 million, the loss is more than 300million, and the proposed fund-raising is 4billion
Try catch of Bash
[6. high precision multiplication]
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
Extending kubernetes API with CRD
In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
Global exception handling
[proteus simulation] INT0 and INT1 interrupt count
Day12QFile2021-09-27
银联支付 返回商户 Nignx post请求405
Annual special analysis of China Mobile Banking in 2022
Implementation differences between import and require in browser and node environments