当前位置:网站首页>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 .
边栏推荐
- [learning notes] node -- from 0 foundation to actual enterprise official website
- Tsinghua, air, Tencent | 3D isovariant molecular map pre training
- Is it safe for Huatai Securities to open an account online? Is it true
- 外地人在华泰开户安全吗?会被骗吗
- 网络通信原理与IP地址的分配原理,网络七层由下往上分别为物理层、数据链路层、网络层、传输层、会话层、表示层和应用层
- 【机器学习基础】特征工程常用操作
- 隐藏网站服务器响应头中 PHP 版本信息
- Qt报错:错误 C2039 “Value“: 不是 “`global namespace‘“ 的成员
- Interviewer: explain the core principle of ThreadLocal
- switch语句的工作原理
猜你喜欢

Leetcode 1074. number of submatrices that sum to target

十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化

数学向量基本知识

系统安全测试要怎么做,详细来说说

从业务开发中学习和理解架构设计

Use modern development methods and thinking to get rid of the "stumbling block" of legacy systems

Ten years of sharpening a sword, the core technology evolution of the cloud native distributed database polardb-x

亿级融资事件占比超30%,超自动化的下一站是何处?丨曼孚科技

网络通信原理与IP地址的分配原理,网络七层由下往上分别为物理层、数据链路层、网络层、传输层、会话层、表示层和应用层

What are you doing at two in the morning?
随机推荐
目前都有哪些年利率6%左右的保本理财产品?
Interviewer: explain the core principle of ThreadLocal
The gospel of small and medium-sized enterprises is coming! Jnpf is becoming popular, helping business digital upgrading
【C语言基础】15 位运算
BGP reflector, federal, routing rules
电脑一直按键如何处理
【Node中间层实践小记(一)】----搭建项目框架
Is it safe for CITIC futures to open an account online and will it be cheated?
【PyTorch】cuda()与to(device)的区别
Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
博世BOSCH EDI项目案例
射频电路循证设计
Technology sharing | big transaction blocking show master status
Distributed lock optimization scheme under 100 million traffic! It works so well~
系统安全测试要怎么做,详细来说说
凌晨两点,你们都在卷什么?
十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
This tool complements the last kilometer of JMeter performance analysis
d类型不同的模板错误
华泰证券网上开户安全吗是真的吗