当前位置:网站首页>Leetcode problem solving -- 99 Restore binary search tree
Leetcode problem solving -- 99 Restore binary search tree
2022-07-06 03:07:00 【Snowy solitary boat】
public void recoverTree(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
TreeNode prev = null;
TreeNode node1=null,node2=null;
while (!stack.isEmpty()||node!=null){
while (node!=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
if (prev!=null&&node.val<prev.val){
if (node1==null){
node1 = prev;
node2 = node;
}else {
node2 = node;
}
}
prev = node;
node = node.right;
}
if (node1!=null&&node2!=null){
int temp = node1.val;
node1.val = node2.val;
node2.val = temp;
}
}
Ideas :
The topic is about switching nodes , In fact, there are only two possibilities , The exchanged nodes are adjacent , Or the exchanged nodes are not adjacent
The exchanged nodes are adjacent :
ai and ai+1 In exchange for , Then there is ai>ai+1 Save two nodes
Can we exchange two nodes directly at this time ? Not at all , Because there is another situation
ai and aj In exchange for , After exchanging
ai>ai+1,aj-1>aj
These two cases are found at the first node , If the situation is the same, we can save the two nodes in the first case , Then, if the next unqualified point appears, it will ai+1 use aj Replace
Then the same switch two nodes at the end of the loop
边栏推荐
- Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
- Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
- Technology sharing | what if Undo is too big
- #PAT#day10
- 2.12 simulation
- Linear programming matlab
- Summary of Bible story reading
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- Some problem records of AGP gradle
- Codeforces 5 questions par jour (1700 chacune) - jour 6
猜你喜欢

My C language learning record (blue bridge) -- on the pointer

深入探究指针及指针类型

Who is the winner of PTA
![[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University](/img/0f/520242492524522c887b6576463566.jpg)
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University

Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition

【概念】Web 基础概念认知

#PAT#day10

Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version

电机控制反Park变换和反Clarke变换公式推导

【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
随机推荐
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
【指针训练——八道题】
C # create self host webservice
主数据管理(MDM)的成熟度
[unity3d] GUI control
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
resulttype和resultmap的区别和应用场景
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
How does yyds dry inventory deal with repeated messages in the consumption process?
A copy can also produce flowers
全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
[kubernetes series] learn the exposed application of kubernetes service security
jsscript
Redis cluster deployment based on redis5
Summary of Bible story reading
Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
RobotFramework入门(一)简要介绍及使用
Maturity of master data management (MDM)
Microservice registration and discovery