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 ...
随机推荐
- 为什么说Kindle难圆“中国梦”? 支撑Kindle模式成功的要素,在当下中国并不太具备
http://www.huxiu.com/article/12993/1.html 6月7日更新:Kindle入华终于尘埃落定,苏宁将在下午4点在北京30家店面同时销售,首批产品为Kindle Pap ...
- oracle 复杂的查找用法
[第一题]: 找到员工表中工资最高的前三名,要求按如下格式输出(第一步部分):以及oracle查询结果指定分页显示的方法(第二部分). ——涉及Top-N分析问题. 一般不在子查询中使用order b ...
- Python zip Python zip函数
zip([iterable, ...])zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的li ...
- 纹理mag filter不能取GL_XXX_MIPMAP_XXXX
今天遇到OpenGL error 0x0500错误,定位到 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 查看ma ...
- memcache基础知识-stats参数
安装memcache: #tar -xvf libevent-1.4.13-stable.tar.gz#cd libevent-1.4.13-stable#./configure && ...
- NGUI 取ScrollView中遮罩区域4个点
用panel.localCorners而不是panel.finalClipRegion,Region还要再换算 首先通过ScrollView取panel,然后取Corners,它返回值代表4个点,映射 ...
- makefile之short函数
函数名称:排序函数-$(sort LIST) 函数功能:给字串"LIST"中的单词以首字母为准进行排序(升序),并去掉重复的单词. 返回值:空格分割的没有重复单词的字串. 函数说明 ...
- 对java中hashmap深入理解
1.HashMap的结构是怎样的? 二维结构,第一维是数组,第二维是链表 2.Get方法的流程是怎样的? 先调用Key的hashcode方法拿到对象的hash值,然后用hash值对第一维数组的长度进行 ...
- Ream的入门使用
一.介绍 Realm是一个不错的手机平台上的数据库,支持多种编程环境,如:Java.Object-C.React Native.Swift.Xamari等. Realm的官网:https://real ...
- SAML2.0 协议初识(二)---Service Provider(SP)
上一节,我们初步认识了 SAML 协议的概念和工作流程,这一节将介绍 SP 端的一些细节. 通常情况下,SP 端是请求发起端,即当用户访问 SP 端的受保护资源时,由 SP 端向认证中心(IDP 端) ...