当前位置:网站首页>剑指Offer 57. 二叉树的下一个节点
剑指Offer 57. 二叉树的下一个节点
2022-06-21 16:53:00 【SS_zico】
牛客 :JZ57
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
分析二叉树的下一个节点,一共有以下情况:
1.二叉树为空,则返回空;
2.节点右孩子存在,则设置一个指针从该节点的右孩子出发,一直沿着指向左子结点的指针找到的叶子节点即为下一个节点;
3.节点不是根节点。如果该节点是其父节点的左孩子,则返回父节点;否则继续向上遍历其父节点的父节点,重复之前的判断,返回结果。代码如下:
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;
}
};
边栏推荐
- C语言dll动态链接库
- PHP连接Mysql8.0报错:Illuminate\Database\QueryException
- TypeScript编译生成文件对比
- How can multidimensional analysis pre summary work?
- EtherCAT igh function attempt
- 快速失败和安全失败的区别
- Structural modeling and Design -- a practical summary of the functions of rotary cutting and fillet chamfering in feature forming of SolidWorks software (drawing a countersunk screw hole)
- Simulation Implementation of list
- Typescript的复合类型
- 服务端socket程序
猜你喜欢

论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》

Operation of simulation test platform for test questions bank of R1 quick opening pressure vessel operation certificate in 2022

Runmaide medical passed the listing hearing: it is expected that the loss will increase, with huoyunfei brothers holding about 33%

缓存型数据库Redis的配置与优化

Byte Jump propose un nouveau type de réseau léger et efficace, mobovit, qui surpasse GhostNet et mobilenetv3 dans la classification, la détection et d'autres tâches CV!

I got a pay cut in disguise

雷军的千亿失误?

Fishman: telecom customer churn prediction game scheme!

SQL操作:WITH表达式及其应用

Win32com operation Excel
随机推荐
EtherCAT igh源码的ecrt_slave_config_dc()函数的理解。
C语言与Lua的交互(实践三)
From demand to open source, how to look at it with new eyes?
Simulation of vector
Zhong'an insurance, together with Alibaba health and huiyitianxia, explores a new model of Internet chronic disease management
智慧三农数字王宁:适合初学者的前5日交易秘诀
RT thread persimmon pie M7 Quanzhi f133 DDR running xboot
Excess rlsp
科普大佬说 | 如何打造自己的AI创造力?
Node的url模块
Vue.js+Node.js全栈开发教程:连接MySQL
On two years' study experience in University
aws elastic beanstalk入门之简介
C language DLL Dynamic Link Library
Window常用快捷键
Deeply understand the attention mechanism of map
Fishman: telecom customer churn prediction game scheme!
Byte traffic business experience: realize as soon as possible, sew money bags, and sell all in goods
Seventy years of neural network: review and Prospect
数字藏品系统开发,NFT艺术品交易平台搭建