当前位置:网站首页>面试题 04.06. 后继者-dfs加辅助变量遍历
面试题 04.06. 后继者-dfs加辅助变量遍历
2022-07-16 01:14:00 【Mr Gao】
面试题 04.06. 后继者
设计一个算法,找出二叉搜索树中指定节点的“下一个”节点(也即中序后继)。
如果指定节点没有对应的“下一个”节点,则返回null。
示例 1:
输入: root = [2,1,3], p = 1
2
/
1 3
输出: 2
示例 2:
输入: root = [5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/
2 4
/
1
输出: null
解题代码如下:
/** * 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;
}
边栏推荐
- [C language] string function and memory operation function
- 迅为龙芯开发板国产双核64位Loognix系统双千兆以太网更多接口
- 深度学习------验证码
- Unity (c) method for obtaining the encoding format of files
- 程序员头疼的 4 种原因 | 每日趣闻
- Redis五种常用数据类型有哪些?对应的数据存储空间又是怎样的?带你从零开始学习
- Assembly language instruction Encyclopedia
- Gin框架的使用
- [LSTM regression prediction] Based on MATLAB tpa-lstm time attention mechanism, long-term and short-term memory neural network regression prediction (multiple input and single output) [including Matla
- 【YOLO】v5s 6.1版本LoadStreams类源码解读
猜你喜欢
随机推荐
WinForm + C GDI + programming to realize Photoshop, illustrator similar drawing toolbox
Celsius的破产全过程是怎样的?这篇文章带你亲身体会
Professional qualities of network security practitioners
Analysis of common interview questions in MySQL
语音芯片ic分类以及sop8的otp语音芯片对比 选型
Assembly language instruction Encyclopedia
力扣(LeetCode)196. 删除重复的电子邮箱(2022.07.15)
一道极限题目
Image format analysis
Redis data type
thinkphp 代码执行 (CNVD-2018-24942)
Binary tree traversal
Delete After the idea directory, the SVN menu resumes operation
When Scala reads the input content in the command-line environment, the input content does not display the problem (unresolved)
uniapp uni-popup change
Part 50 - Analysis of a query request header parameter [2022-07-14]
Common audio features: Mel spectrum, amplitude spectrum (short time Fourier transform spectrum /stft), Mel cepstrum (MFCC)
深度学习------不同方法实现resnet-18、resnet34
Critique of A Novel Proof-of-Reputation Consensus for Storage Allocation in Edge Blockchain Systems
Differences among screenwidth, clientwidth, offsetwidth, and scrollwidth









