Leetcode-Recover BST
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
O(n) time and O(1) space solution: Morris Traversal
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class Solution {
public void recoverTree(TreeNode root) {
if (root==null) return; TreeNode pre=null,cur=null,first=null,second=null;
cur = root;
while (cur!=null){
//cur.left is null.
if (cur.left==null){
if (pre!=null && pre.val>cur.val){
if (first==null){
first = pre;
second = cur;
} else second = cur;
}
pre = cur;
cur = cur.right;
} else {
//get predecessor.
TreeNode temp = getPredecessor(cur);
if (temp.right==null){
temp.right=cur;
cur = cur.left;
} else {
if (pre!=null && pre.val>cur.val){
if (first==null){
first = pre;
second = cur;
} else second = cur;
}
temp.right = null;
pre = cur;
cur = cur.right;
}
}
} if (first==null) return; int temp = first.val;
first.val = second.val;
second.val = temp; return;
} public TreeNode getPredecessor(TreeNode cur){
TreeNode pre = cur.left;
while (pre.right!=null && pre.right!=cur){
pre = pre.right;
} return pre;
}
}
O(log(n)) space solution:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Result {
TreeNode pre;
TreeNode first;
TreeNode second;
Result() {
pre = first = second = null;
}
} public class Solution {
public void recoverTree(TreeNode root) {
Result res = new Result();
recoverTreeRecur(root,res);
if (res.first!=null && res.second!=null){
int temp = res.first.val;
res.first.val = res.second.val;
res.second.val = temp;
}
} public void recoverTreeRecur(TreeNode cur, Result res){
if (cur==null)
return; recoverTreeRecur(cur.left, res);
if (res.pre==null) res.pre = cur;
else if (res.pre.val>cur.val){
if (res.first==null)
res.first = res.pre;
res.second = cur;
} res.pre = cur;
recoverTreeRecur(cur.right,res);
}
}
Leetcode-Recover BST的更多相关文章
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Leetcode] Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- [LeetCode] Split BST 分割二叉搜索树
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode: Recover Binary Search Tree [099]
[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without cha ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- Rabbitmq消息队列(三) 工作队列
1.简介 默认来说,RabbitMQ会按顺序得把消息发送给每个消费者(consumer).平均每个消费者都会收到同等数量得消息.这种发送消息得方式叫做——轮询(round-robin). 工作队列(又 ...
- python-循环while
while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...
- Python zip Python zip函数
zip([iterable, ...])zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的li ...
- mysql-group-replication 测试环境的搭建与排错
mysql-group-replication 是由mysql-5.7.17这个版本提供的强一致的高可用集群解决方案 1.环境规划 主机ip 主机名 172.16.192.201 balm001 17 ...
- windows server 2003中端口默认不能使用问题
问题:在windows server 2003中IIS6.0新建站点,给了一个新端口(非80),然后配置好后不能访问 解决方案:系统内置防火墙需要添加对应端口,如下图: 即解决.
- mmcm 和pll
这个2个有什么区别啊 mmcm 和pll? 1.DCM实际上就是一个DLL,可以对输入时钟进行相位移动,补偿,产生倍频和分频时钟,但是5以及以后的产品不用了.2.PLL相对于DCM,除了不能相移时钟 ...
- 阿里大鱼短信接口(Python3版)
近期由于须要用到短信接口,选型的的结果是用阿里大鱼的短信服务,然而淘宝开放平台(TOP)的SDK已经非常多年没有更新了.不支持python3.自己动手改了半天,还是不太正常,索性不用它,自己写一个算了 ...
- [Egret]长按截屏分享、分享截屏图片、本地存储
egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...
- 李洪强和你一起学习前端之(7)定位盒子 css可见性 滑动门案例
今天是2017年3月23日 1 复习昨天知识 1.1浮动 Float:left | right 特点: ->浮动的元素不占位置(脱标) ->可以将行内元素转化为行内块元素 ->块级元 ...
- 初识md5碰撞与crc32碰撞
现在是晚上23:29.写这篇文章呢,是因为早些时候我胃疼,是因为凉导致的胃疼.凉呢喝了一些热水,喝完热水胃倒是不疼了,但是由于我喝的是茶叶开水,于是就导致失眠了.想来想去这漫漫长夜也没意思,于是就决定 ...