当前位置:网站首页>Leetcode-99. restore binary search tree
Leetcode-99. restore binary search tree
2022-07-23 10:09:00 【Technical bricklayer -- Felix】
subject
Give you the root node of the binary search tree root , In the tree just The values of the two nodes are incorrectly exchanged . Please... Without changing its structure , Restore the tree .
Example 1:
Input :root = [1,3,null,null,2]
Output :[3,1,null,null,2]
explain :3 It can't be 1 The left child , because 3 > 1 . In exchange for 1 and 3 Make the binary search tree efficient .
Example 2:

Input :root = [3,1,4,null,null,2]
Output :[2,1,4,null,null,3]
explain :2 Can't be in 3 In the right subtree , because 2 < 3 . In exchange for 2 and 3 Make the binary search tree efficient .
Tips :
The number of nodes on the tree is in the range [2, 1000] Inside
-231 <= Node.val <= 231 - 1
Advanced : Use O(n) The solution of space complexity is easy to realize . You can think of one that only uses O(1) Space solutions ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/recover-binary-search-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
package org.lht.boot.lang. Algorithm .leetcode;
import java.util.ArrayList;
import java.util.List;
/** * Description: https://leetcode.cn/problems/recover-binary-search-tree/ * * @Author lht * @Date 2022/7/13 21:00 **/
public class L99 Restore binary search tree {
public static void main(String[] args) {
TreeNode root = new TreeNode();
//.. Defining parameters
recoverTree(root);
}
/** * transformation * @param root */
public static void recoverTree(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
int[] index = findIndex(list);
recoverTree(root, 2, index[0], index[1]);
}
/** * According to the result of middle order traversal , Find the two exchanged node values . * @param nums * @return */
public static int[] findIndex(List<Integer> nums) {
int preIndex = -1;
int nextIndex = -1;
for (int i = 0; i < nums.size() - 1; i++) {
if (nums.get(i) > nums.get(i + 1)) {
preIndex = i + 1;
if (nextIndex == -1) {
nextIndex = i;
} else {
break;
}
}
}
int x = nums.get(preIndex), y = nums.get(nextIndex);
return new int[]{
x, y};
}
/** * Exchange the two found nodes , Exchange come here * @param root * @param count * @param x * @param y */
public static void recoverTree(TreeNode root, int count, int x, int y) {
if (root != null) {
if (root.val == x || root.val == y) {
root.val = root.val == x ? y : x;
if (--count == 0) {
return;
}
}
recoverTree(root.left, count, x, y);
recoverTree(root.right, count, x, y);
}
}
/** * In the sequence traversal * * @param root * @param nums */
public static void inorder(TreeNode root, List<Integer> nums) {
if (root == null) {
return;
}
inorder(root.left, nums);
nums.add(root.val);
inorder(root.right, nums);
}
}
Their thinking
This question is actually very simple , There are several ideas
First of all , The result of traversal using middle order is ordered .
second , The two nodes in the binary search tree are exchanged , The structure of the tree has not changed
Third , And only two nodes are exchanged , So you can find two swapped nodes .
Fourth , Switching nodes , Do not operate on tree results , Therefore, any kind of traversal can be used .

take 7 and 4 In exchange .
边栏推荐
- Qt::WA_TransparentForMouseEvents 了解一下
- 转行软件测试薪资10K | 手中有粮心中有底,在任何时候都是真理
- 十年架构五年生活-05第一次出差
- 实现多层级条件查询(类似京东多层级添加查询)
- 【机器学习基础】特征工程常用操作
- 《nlp入门+实战:第一章:深度学习和神经网络》
- 141.环形链表
- leetcode-99.恢复二叉搜索树
- PHP converts ASCII code to string, and string converts ASCII code
- Tsinghua, air, Tencent | 3D isovariant molecular map pre training
猜你喜欢

组合数。。。。

leetcode 1074. Number of Submatrices That Sum to Target(和为target的子矩阵个数)

L-半胱氨酸修饰的金纳米粒子(Cys-GNPs)和牛血清白蛋白/生物素化白蛋白纳米粒

枚举类的使用和实现

Qt报错:错误 C2039 “Value“: 不是 “`global namespace‘“ 的成员

范式及反范式

Click position and click offset of airtest script

《nlp入门+实战:第一章:深度学习和神经网络》

60 open-ended test questions, recite them and get a pay rise directly

Distributed lock optimization scheme under 100 million traffic! It works so well~
随机推荐
【学习笔记】Node--从0基础到实战企业官网
What are you doing at two in the morning?
How to do the system security test? Let's talk about it in detail
Perlin 噪声与随机地形
Hide the PHP version information in the response header of the website server
数组中的逆序对
Click position and click offset of airtest script
PHP converts pictures to Base64 format and restores them to generate pictures
spark分区算子partitionBy、coalesce、repartition
数据库安全性和数据的完整性
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
PHP script paging TXT content case
Is it safe for CITIC futures to open an account online and will it be cheated?
How to learn SCM based on zero?
【Azure 事件中心】Azure Event Hub 新功能尝试 -- 异地灾难恢复 (Geo-Disaster Recovery)
平台型企业如何解决分账核算业务呢?
Spark 内存管理机制 新版
华泰证券开户安全性高吗,怎么办理
线性代数之二阶与三阶行业式
范式及反范式