当前位置:网站首页>Restore the binary search tree [simulate according to the meaning of the question - > find the problem - > analyze the problem - > see the bidding]
Restore the binary search tree [simulate according to the meaning of the question - > find the problem - > analyze the problem - > see the bidding]
2022-06-29 03:19:00 【REN_ Linsen】
Problem solving process
Preface
Too many algorithm questions , Knowledge points have also accumulated , At this time, we need to see what we can do , Flexible combination of knowledge points . But I want to make a move , You have to know what the topic is about , If you can't find out what the trick is , How to talk about defying tactics ?
General process of problem solving : According to the meaning of the question, simulate and understand -> Find the problem -> To analyze problems ( Sorting )-> See following .
One 、 Restore binary search tree


Two 、 See following
package everyday.medium;
import java.util.ArrayList;
import java.util.List;
// Restore binary search tree
public class RecoverTree {
/* target: There are two nodes that do not satisfy the size relationship , Exchange it . I haven't analyzed the subject before , Only simple traversal , Find a situation that varies from front to back , Just swap it , As it says target. summary : I didn't settle down to analyze the problem , Fully understand the topic , So as to open the right path of analysis , Don't pay attention to details , The direction cannot be opened . The analysis is as follows , Exchange two elements , This will result in two decrements in the medium order increasing sequence , Set the first place a > b; The second place c > d; Put the smallest d And the biggest a In exchange for , The sequence becomes incremented . Of course , If it is two adjacent , There is only one difference , set up a > b, Put the smallest b And the biggest a In exchange for , The sequence becomes incremented . Record decreasing 4/2 Nodes , Exchange according to the situation . */
public void recoverTree(TreeNode root) {
// In the sequence traversal , Looking for diminishing 4/2 Nodes , Put in list In .
inOrder(root);
// Deal with the exchange according to the situation .
// Let what needs to be exchanged be x and y
TreeNode x = error.get(0), y = error.size() == 2 ? error.get(1) : error.get(3);
// In exchange for x and y The value of the can , No need to change the structure .
// swap
int t = x.val;
x.val = y.val;
y.val = t;
}
List<TreeNode> error = new ArrayList<>();
TreeNode pre = null;
private void inOrder(TreeNode root) {
// Reach empty node , End recursion .
if (root == null) return;
// Traverse left subtree
inOrder(root.left);
// In the sequence traversal .
if (pre != null && pre.val > root.val) {
error.add(pre);
error.add(root);
}
// Update forerunner .
pre = root;
// Traversal right subtree .
inOrder(root.right);
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
summary
1) Restore binary tree , Why do wrong binary trees appear ? Ask key questions .
2) General process of problem solving : According to the meaning of the question, simulate and understand -> Find the problem -> To analyze problems ( Sorting )-> See following .
reference
边栏推荐
- PAT甲级 A1057 Stack
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- Jerry's watch stops moving [chapter]
- Tu ne peux pas comprendre le feu?
- Getting started with testing - integration testing
- For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
- 2022-2028 global bubble CPAP system industry survey and trend analysis report
- Tortoise 没有显示绿色图标
- Jerry's monitoring alarm clock [chapter]
- How to skip time when closing a socket connection_ Wait status of wait
猜你喜欢

逆序对对数计算,顺序对对数计算——归并排序

深入解析 Apache BookKeeper 系列:第三篇——读取原理

设备监理师证书含金量怎样?值得考吗?

How to optimize databases and tables

Altium Designer中从已有的PCB中导出所有元件的封装的方法

Tupu software intelligent energy integrated management and control platform

初探元宇宙存储,数据存储市场下一个爆点?
![[线性代数] 1.1 二阶与三阶行列式](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[线性代数] 1.1 二阶与三阶行列式

解决allegro中测量距离时,点击一个点后光标闪烁的问题
![[yunyuanyuan] it's so hot. Why don't you come and understand it?](/img/a8/99037ec5b796e39b9e76eac95deb86.png)
[yunyuanyuan] it's so hot. Why don't you come and understand it?
随机推荐
1110: 最近共同祖先(函数专题)
gcc编译器包
Web APIs 高阶函数 丨黑马程序员
2022-2028 global pneumatic test probe industry survey and trend analysis report
LinkedList learning
不同的二叉搜索树[自下而上回溯生成树+记忆搜索--空间换时间]
MATALB signal processing - signal transformation (6)
Altium Designer中从已有的PCB中导出所有元件的封装的方法
How to optimize databases and tables
Yyds dry inventory everything a primary developer should know about activity
Codeforces Round #771 (Div. 2) ABC
Probe into metacosmic storage, the next explosive point in the data storage market?
Is the account opening of GF Securities really safe and reliable
Want to be an equipment manager? If you meet these three conditions, you can
The continued movement of Jerry's watch [chapter]
Equal wealth
Problème - Ajouter shellerror: permissions d'instrumentation pour le périphérique: vérifier les règles udev.
Setting alarm mode of Jerry's watch [chapter]
Allegro's method of setting network flying line and network color
[yunyuanyuan] it's so hot. Why don't you come and understand it?