当前位置:网站首页>Interview question 04.06 Successor DFS plus auxiliary variable traversal
Interview question 04.06 Successor DFS plus auxiliary variable traversal
2022-07-18 12:08:00 【Mr Gao】
Interview questions 04.06. The successor
Design an algorithm , Find the... Of the specified node in the binary search tree “ next ” node ( That is to say, the middle order follows ).
If the specified node does not have a corresponding “ next ” node , Then return to null.
Example 1:
Input : root = [2,1,3], p = 1
2
/
1 3
Output : 2
Example 2:
Input : root = [5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/
2 4
/
1
Output : null
The solution code is as follows :
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int r;
struct TreeNode* re;
void order_search(struct TreeNode* root,struct TreeNode* p){
if(root&&r!=2){
order_search(root->left,p);
if(r==1){
re=root;
r=2;
}
if(root==p&&r==0){
r=1;
}
order_search(root->right,p);
}
}
struct TreeNode* inorderSuccessor(struct TreeNode* root, struct TreeNode* p){
r=0;
re=NULL;
order_search(root,p);
return re;
}
边栏推荐
- Installing MySQL on Linux
- 为何消费者都不愿买国产而买iPhone?因为国产手机贬值太快了
- Mid platform construction sharp weapon SPI plug-in mechanism
- 常用音频特征:梅尔频谱(melspectrogram)、幅度谱(短时傅里叶变换谱/STFT)、梅尔倒谱(MFCC)
- Excel-VBA 快速上手(七、获取单元格对象)
- Matlab drawing_ 1 draw attenuation oscillation curve
- Solution to script loss when the prefab of unity ngui is migrated to other projects
- Go automatic execution command record
- Ftxui basic notes (Hello World)
- 【VSCode】切换文件与编辑器的快捷键
猜你喜欢

迁移学习模型训练一

ThreadLocal夺命11连问 我实在扛不住呀

重建二叉树

2022年7月16日CDGA/CDGP数据治理认证考试成绩出来啦!

Power BI----DAX讲解

UE4_ Ue5 play audio (play, stop function) (attached project)

【转载】LaTeX中的空格汇总

The sandbox alpha Season 3 first Trailer

Solution to script loss when the prefab of unity ngui is migrated to other projects

What are the five common data types of redis? What is the corresponding data storage space? Take you to learn from scratch
随机推荐
JVM调优实战(详细版)
如何使用Jedis操作Redis数据库?
Solution to script loss when the prefab of unity ngui is migrated to other projects
力扣(LeetCode)196. 删除重复的电子邮箱(2022.07.15)
Power Bi ---- what is a measure?
如何在TIA 博途中计算终端设备(例如水泵)的运行时间(附FB库文件)?
Traditional gyms are trapped in large-scale, and Lexus sports "s2b2c" mode is the reference answer?
Summarize the differences between i++ and i++
Analysis of common interview questions in MySQL
迁移学习模型训练二
MySQL original field to hump naming
[load balancer does not contain an instance for the service mall coupling] and the project start normally but cannot register with Nacos
Résolution du format d'image
Unity (c) method for obtaining the encoding format of files
我的两周年创作纪念日
GDB或delve调试Go程序查看变量显示<optimized out>解决办法
mysql原字段转驼峰命名
自动推理的逻辑01
Why do consumers buy iPhones instead of domestic ones? Because the depreciation of domestic mobile phones is too fast
Image format analysis