当前位置:网站首页>Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
2022-07-28 22:20:00 【Calm in the wind and rain】
The finger of the sword Offer II 053. Middle order successor in binary search tree
Given a binary search tree and one of its nodes p , Find the sequential successor of this node in the tree . If the node has no middle order successor , Please return null .
node p The following is the value ratio p.val The node with the smallest key value among the large nodes , That is, the order nodes traversed in the middle order p The next node of .
Example 1:

Input :root = [2,1,3], p = 1
Output :2
explain : here 1 The middle order of is followed by 2. Please note that p And the return value should be TreeNode type .
Example 2:

Input :root = [5,3,6,2,4,null,null,1], p = 6
Output :null
explain : Because the given node has no middle order successor , So the answer goes back to null 了 .
Tips :
The number of nodes in the tree is in the range [1, 104] Inside .
-105 <= Node.val <= 105
The values of each node in the tree are guaranteed to be unique .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/P5rCT8
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Using the characteristics of binary search tree , The values of the nodes on the left subtree of a node of the tree are smaller than those of the current node , The value of the node on the right subtree is greater than that of the current node , Then just keep following p Compare the values of and consider whether to traverse left or right .
Answer key (Java)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode ans = null;
while (root != null) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
root = root.right;
}
}
return ans;
}
}
边栏推荐
- 深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
- Differences of display values
- 什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
- [machine learning] naive Bayesian classification of text -- Classification of people's names and countries
- Bugku, Web: all filtered
- 数据可视化新闻,不一样的新闻报道形式
- 科大讯飞笔试
- 网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
- HCIP(9)
猜你喜欢

PCB材料简单介绍
![[machine learning] naive Bayesian classification of text -- Classification of people's names and countries](/img/95/1f5b0a17a00da5473180667ccc33e2.png)
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries

internet的基本服务中文件传输命令是哪个

腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实

SQL injection less34 (post wide byte injection + Boolean blind injection)

Alibaba cloud CDN practice

腾讯云数据库负责人借了一亿元炒股?知情人士:金额不实

HCIP(11)

Add DNS server to LAN for domain name resolution

科大讯飞笔试
随机推荐
Esp8266 Arduino programming example - timer and interrupt
Written examination summary record
Part 8: creating camera classes
If you want to grow rapidly, you must first experience a major blow!
hcip实验(14)
Aimbetter insight into your database, DPM and APM solutions
Win11怎么打开软件通知
What is the difference between inline elements and block level elements? Semantic function
HCIP(11)
HCIP第七次实验
Esp8266 Arduino programming example - SPIFs and data upload (Arduino IDE and platformio IDE)
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
熊市下 DeFi 的未来趋势
ECMASript 5/6 笔记
PCB材料简单介绍
Learn kotlin - extension function
LCR测试仪最为主要的功能和用途都是什么
The difference between get and post
什么是时间复杂度
HCIP(9)