当前位置:网站首页>Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
2022-07-28 06:39:00 【JETECHO】
Original link (https://leetcode.cn/problems/P5rCT8)
List of articles
Title Description
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
Example 2

Input :root = [5,3,6,2,4,null,null,1], p = 6
Output :nul
Data constraints
- p Is the node of the tree
- The number range of nodes in the number is [1,10^4]
- -10^5 <= Node.val <= 10^5
- The values of each node in the tree are guaranteed to be unique .
Ideas
Binary search tree is a special binary tree , For a node , Its value must be greater than any value of its left subtree , Any value of its right subtree must be greater than it , Several left subtrees < Parent node < Right subtree . It is not necessary to scan every node when using this feature to find a binary tree , Since the topic wants to find the smallest one larger than it, then if you can walk the left subtree, you must walk the left subtree , After walking to the right subtree, you also need to check the left subtree, otherwise it is the right subtree . If the right subtree does not match in the end, then no data matches . At the same time, because the need to match is a tree node , Then if this node has a right subtree, it must be the node from the right subtree to the left subtree , Otherwise, you need to go from the root node .
Such as example 2 The access process of should be
First p No right subtree, all from scratch 
Because the value of the root node is less than 6 So you should take the right subtree 
Finally get null
Suppose that the example 2 Of p Change it to 3 The result of this node is different
First ,3 There is a right subtree, so you can go through the right subtree 
Then the right subtree has no left subtree, so we get 4
Suppose there is a binary search tree, as shown in Figure 
hypothesis P by 50
First p There is no right subtree, so you need to start from the root node .
First, the value of the root node is less than 50 So you need to go to the right subtree , Now walk to 65 Node 
65 Greater than 50 So you need to go left , But the value of the left subtree is 50 No more than 50 So Zuozi tree also came to the end , So we get the result 65
Code
/** * 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;
if (p.right != null) {
ans = p.right;
while (ans.left != null) {
ans = ans.left;
}
}
while (root != null) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
root = root.right;
}
}
return ans;
}
}
边栏推荐
- Leetcode 刷题日记 剑指 Offer II 048. 序列化与反序列化二叉树
- Dynamic programming -- simple question type of climbing stairs
- Feignclient @requestmapping parameter setting and simple method setting of request header
- QT implementation outputs relevant information to log file
- OJ 1131 beautiful number
- 刷题记录----链表
- Bug experience related to IAP jump of stm32
- OpenGL development environment configuration [vs2017] + frequently asked questions
- Use and safe shutdown of qthread thread in QT
- 2022-05-15 基于jwt令牌token
猜你喜欢

Solve the problem that the memory occupation is higher than that of the application process

Problem solving for ACM freshmen in Jiangzhong on October 26

Icc2 (IV) routing and postroute optimization

【C笔记】数据类型及存储

What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended

夹子套利/搬砖套利系统开发

Use and safe shutdown of qthread thread in QT

气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜

关于Shader KeyWord的整理

QT painting event - set background picture
随机推荐
2022-06-07 responsebodyadvice caused the spring frame problem in swagger
2022-06-07 VI. log implementation
Solve the problem that the memory occupation is higher than that of the application process
万字归纳总结并实现各大常用排序及性能对比
SSAO By Computer Shader(三)
OJ 1505 保险丝
从普通查询商品到高并发查询商品的优化思路
C语言的文件操作
图形管线基础(二)
2022-05-24 use of spiel
OJ 1045 reverse and add
Feignclient @requestmapping parameter setting and simple method setting of request header
【C语言】动态内存管理
Development of Quantitative Trading Robot System
2022-05-15 based on JWT token
Leetcode 刷题日记 剑指 Offer II 047. 二叉树剪枝
Leetcode 刷题日记 剑指 Offer II 048. 序列化与反序列化二叉树
gerapy 使用
气传导耳机哪个品牌比较好、这四款不要错过
What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended