当前位置:网站首页>[leetcode] restore binary search tree
[leetcode] restore binary search tree
2022-06-11 01:50:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of binary tree 】
- Restore binary search tree
https://leetcode-cn.com/problems/recover-binary-search-tree/ - analysis
An important characteristic of binary search tree is that the result of its order traversal is ascending , The binary search tree is swapped for two nodes , It is equivalent to that two numbers are exchanged in the ascending array . How to find two numbers exchanged in an ascending array ?
Suppose an array [1,2,3,4,5,6,7] Swap one of ,2、6 by [1,6,3,4,5,2,7],
When the current element is larger than the value of the next element for the first time, it must be the first position of the error ( One of the elements being exchanged ), Set to error1, Then the next step is to find error1 In the right place . The above example is 6>3,error1 by 6.
Keep going back , For the first time, it is better than error1 Big elements , His last one was error2 The location of , That is, the second element that went wrong ( Two of the elements exchanged ). The above example is 7>6,error2 by 7 Former one 2. Of course, if you don't find anything better than error1 Big elements , Then he is the biggest , It should be at the end of the array . - Realization
public void recoverTree(TreeNode root) {
TreeNode node = root;
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode error1 = null, error2 = null, prev = null;
// In the sequence traversal
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.left;
}
node = stack.pop();
if (prev != null && error1 == null && node.val < prev.val) {
// Wrong page 1 Nodes , First appearance i Less than i-1,i-1 Must be the first wrong value
error1 = prev;
}
// find error2 Where it should be , That is, the first occurrence greater than error1 The value of , The previous one should be error2 The location of
// If not , It must be the last one
if (error1 != null && node.val > error1.val) {
error2 = prev;
break;
}
prev = node;
node = node.right;
}
// In exchange for
// error2 It's empty , Then the last one is error2
if (error1 != null) {
error2 = error2 == null ? prev : error2;
int value = error1.val;
error1.val = error2.val;
error2.val = value;
}
}
LeetCode Time consuming :1ms
- summary
- Master the realization of middle order traversal , Including recursive implementation and non recursive implementation
- The binary search tree is followed by an ascending array
- After minimizing the problem , First, simulate more situations , ideas , Do it again
边栏推荐
- [leetcode] delete duplicate Element II in the sorting linked list
- 关于CS-3120舵机使用过程中感觉反应慢的问题
- threejs:两点坐标绘制贝赛尔曲线遇到的坑
- A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
- 2021-2-14 gephi learning notes
- 關於概率統計中的排列組合
- threejs:如何获得几何体的boundingBox?
- Leetcode 2171 removing minimum number of magic beans (prefix and recommendation)
- LeetCode 1024 Video Stitching (dp,jump game)
- 2021-02-03 learning notes of MATLAB before the US games (grey prediction and linear programming)
猜你喜欢
![[leetcode] reverse linked list II](/img/03/5e4921a8fe50b3489b4b99310d1afb.jpg)
[leetcode] reverse linked list II
![[leetcode] ordered linked list transformation binary search tree](/img/9f/86e819beb8dc678d79c3e307891402.jpg)
[leetcode] ordered linked list transformation binary search tree

从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版

Middleware_ Redis_ 05_ Persistence of redis
![[path planning] week 1: hodgepodge](/img/80/074b847c6826b306318aeb9866a829.jpg)
[path planning] week 1: hodgepodge

Loki learning summary (1) -- the best choice of Loki small and medium-sized project log system

Linux安装mysql数据库详解

1.4PX4程序下载
![[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV](/img/fe/04d95b8d983f491b8e8d4bae7d07ca.jpg)
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV

LeetCode 1609 Even Odd Tree (bfs)
随机推荐
2022.6.6-----leetcode. seven hundred and thirty-two
Negative number +0+ positive number
[leetcode] merge K ascending linked lists
Record the packaging of the googlechrome browser plug-in
Leetcode divide and conquer method
Threejs: pit encountered in drawing Bessel curve with two-point coordinates
LeetCode 1024 Video Stitching (dp,jump game)
[mavros] mavros startup Guide
1.6 Px4 initialization calibration
[path planning] week 1: Path Planning open source code summary (ROS) version
Threejs: streamer effect encapsulation
Leetcode 652 find duplicate subtrees (recommended by DFS)
Leetcode 1248 count number of nice subarrays
【ROS】ROSmsg cakin_ Make compilation error
[leetcode] reverse linked list
Matlab random function summary
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
Kubernetes binary installation (v1.20.15) (VII) plug a work node
Some records of China open SSL compilation
detectron2训练自己的数据集和转coco格式