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 ...
随机推荐
- 【转】可在广域网部署运行的即时通讯系统 -- GGTalk总览(附源码下载)
原文地址:http://www.cnblogs.com/justnow/p/3382160.html (最新版本:V6.0,2017.12.11 .即将推出Xamarin移动端版本,包括 Androi ...
- LNMP架构二
Nginx默认虚拟主机 1.首先修改nginx.conf文件,删除server及下面的,在http最后添加include vhost/*.conf; (指定虚拟主机目录,并读取以.conf结尾的文件) ...
- Spring技术内幕:Spring AOP的实现原理(三)
生成SingleTon代理对象在getSingleTonInstance方法中完毕,这种方法时ProxyFactoryBean生成AopProxy对象的入口.代理对象会封装对target目标对象的调用 ...
- zookeeper(一):功能和原理
简介 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅.负载均衡.命名服务.分 ...
- ui-router参数传递
基本参数: ‘/user/:id' '/user/{id}' '/user/{id:int}' 使用正则表达式: '/user/{id:[0-9]{1,8}' '/user/{id:.*}' '/us ...
- atitit.系统托盘图标的设计java swing c# .net c++ js
atitit.系统托盘图标的实现java swing c# .net c++ js 1. 系统托盘图标的结构 1 2. Java swing的实现 1 3. .net的实现 1 4. C++的实现 1 ...
- MyEclipse中背景颜色的设定
设置代码编写区域背景色的方法: Window-->Preferences-->General-->Editors-->TextEditors-->在Appearance ...
- CentOS 7下Java的SecureRandom种子初始化失败解决办法
io.netty.util.internal.ThreadLocalRandom getInitialSeedUniquifierWARNING: Failed to generate a seed ...
- Nginx配置教程
1. Nginx相关概念 1.1 反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返 ...
- Python中sort以及sorted函数初探
sorted(...) Help on built-in function sorted in module __builtin__: sorted(...) sorted(iterable, cmp ...