【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)的解法. 交换的情况只有两 ...
随机推荐
- javascript笔记5之流程控制语句
/* var box = 100; //单行语句 var age = 20; //另一条单行语句 { //用花括号包含的语句集合,叫做复合语句,单位一个 //一对花括号,表示一个复合语句,处理时候,可 ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- [Cycle.js] Making our toy DOM Driver more flexible
Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent ...
- linux高级技巧:heartbeat+lvs(一)
1.heartbeat一个简短的引论: Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...
- Java Builder模式 体验(二)
在上篇文章中,对Java Builder模式的使用体验主要是从Builder对构造器改造方面的优秀特性来说的,感觉并没有从Java Builder模式本身的功能和作用去写,因此决定再从Build ...
- ubuntu中安装Docker
系统要求: 必须时64位的系统,内核最低要求是3.10 查看系统内核: $ uname -r 3.11.0-15-generic 获取最新版本打Docker: $ wget -qO- https:// ...
- Windows命令行(DOS命令)教程-4(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_3.html
2. md md是英文make directory(创建目录)的缩写 [功能] 创建一个子目录 [格式] md [C:]path [举例] 用md 建立一个叫做purple的目录 3. cd cd是英 ...
- oracle学习笔记(一)用户管理
--oracle学习第一天 --连接 @后面连接数据库实例,具体连接到那个数据库 conn scott/tiger@MYORA1; --修改密码 passw; --显示用户 show user; -- ...
- VC获取当前程序运行路径
/***************************************************/ /* 函数: 获取当前程序运行的路径 /* 返回: 当前程序运行路径 C:\AAA\BBB\ ...
- C语言中的指针数组和数组指针
代码: #include <iostream> using namespace std; int main(){ ]; ]; cout<<sizeof(a)<<en ...