【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
互换二叉搜索树中两个位置错误的节点。
思路:
预设三个指针pre,p1,p2。p1用来指向第一个出错的节点,p2用来指向第二个出错的节点。
出错情况有两种,即p1和p2相邻,p1和p2不相邻。
中序遍历此二叉树,用pre指向当前节点的上一个节点,如果出错的节点相邻,此时p1应该指向pre,p2应该指向当前节点root。
若出错节点不相邻,则用p1记录第一个出错节点pre,继续遍历到前一个节点pre大于当前节点root时,用p2指向第二个出错节点p2.
代码如下:
class Solution {
public:
TreeNode *pre,*p1,*p2; void run(TreeNode *root){
if(!root)
return;
run(root->left);
if(pre && pre->val > root->val){
if(p1 == NULL){
p1 = pre; p2 = root;
}
else{
p2 = root;
}
}
pre = root;
run(root->right);
} void recoverTree(TreeNode *root) {
if(!root)
return ;
pre = p1 = p2 = NULL;
run(root);
swap(p1->val,p2->val);
}
};
【LeetCode练习题】Recover Binary Search Tree的更多相关文章
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Java for LeetCode 099 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Leetcode#99 Recover Binary Search Tree
原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...
- 第五周 Leetcode 99. Recover Binary Search Tree (HARD)
Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...
随机推荐
- 第23讲 UI_布局 之相对布局
第23讲 UI_布局 之相对布局 .RelativeLayout(相对布局): RelativeLayout(相对布局)是指组件的位置总是相对兄弟组件.父容器来决定的(相对位置),如某个组件的左边右边 ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- Codeforces554C:Kyoya and Colored Balls(组合数学计算+费马小定理)
题意: 有k种颜色,每种颜色对应a[i]个球,球的总数不超过1000 要求第i种颜色的最后一个球,其后面接着的必须是第i+1种颜色的球 问一共有多少种排法 Sample test(s) input o ...
- ViewPager实现页卡的3种方法(谷歌组件)
----方法一:---- 效果图: 须要的组件: ViewPager+PagerTabStrip 布局文件代码: <!--xmlns:android_custom="http://sc ...
- DataBindings 与 INotifyPropertyChanged 实现自动刷新 WinForm 界面
--首发于博客园, 转载请保留此链接 博客原文地址 业务逻辑与界面的分离对于维护与迁移是非常重要的,在界面上给某属性赋值,后台要检测到其已经发生变化 问题: 输入某物品 单价 Price, 数量Am ...
- 使用游标循环进行SQL更新插入的SQL语句
使用SQL中的循环,可以实现许多我们需要的操作,比如SQL更新操作.下面就为您介绍使用游标循环进行SQL更新插入的SQL语句写法,希望对您深入学习SQL更新有所帮助. --开始事务 BEGIN TRA ...
- Linq 关键字
from var lowNums = from num in numbers where num < 5 select num; numbers 是数 ...
- HashMap陷入死循环的例子
//使用这个例子可以模拟HashMap陷入死循环的效果,可能需要执行多次才会出现. 1 package com.hanzi; import java.util.HashMap; public clas ...
- Oracle start with connect by prior 用法
Oracle start with connect by prior 用法 语法: select * from 表名 where 条件1 start with 条件2 connect by pr ...
- iOS图片拉伸技巧—— resizableImageWithCapInsets
http://blog.csdn.net/chaoyuan899/article/details/19811889