当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
![[NLP] generate word cloud](/img/c4/4e9707bba58732a90d1c30312719a3.png)
[NLP] generate word cloud

Aimbetter insight into your database, DPM and APM solutions

Official document of kubevela 1.4.x

hcip实验(15)

迪赛智慧数——折线图(堆叠面积图):2022年不同职业人群存款额占月收入比例排名

HCIP(12)

HCIP(11)

Using Baidu easydl to realize chef hat recognition of bright kitchen and stove

System Analyst

SQL注入 Less42(POST型堆叠注入)
随机推荐
HCIP(12)
Mysql内置函数
HCIP(13)
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
小程序 监听目标节点出现到屏幕中
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
从 Web3到Web2.5,是倒退还是另辟蹊径?
lotus 1.16.0 延长扇区过期时间
[cloud native kubernetes] mapping external service under kubernetes cluster eendpoint
openresty 请求鉴权
Apifox: satisfy all your fantasies about API
静态路由和缺省路由实验
Ecmasript 5/6 notes
TensorFlow Serving 高性能的机器学习模型服务系统
Written examination summary record
SQL injection less38 (Stack Injection)
gprs网络指的是什么
40. 组合总和 II
Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
[NLP] generate word cloud