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 ...
随机推荐
- 使用struct与使用class初始化对象效率对比
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- Jekins部署.net站点
前提 1.你需要一台windows服务 可以装vs的且有重启电脑权限的(具体vs版本根据你的团队决定) 2.下载jekins 安装包 地址:https://jenkins.io/download/ ...
- [Android进阶]Binder学习(初始篇)
Android中Binder学习(初始篇) 本篇博客学习自侯亮的博客.地址为: 红茶一杯话Binder 1 什么是Binder? 简单地说.Binder是Android平台上的一种跨进程交互技术. 该 ...
- tcp_recvmsg 函数具体解释
看了非常多网上关于tcp_recvmsg的文章,感觉解释的不太到位,或者非常多都是空口说白话,昨天分析了一下午tcp_recvmsg.感觉了解了十之八九,如今贴出来和大家分享一下. 须要背景:了解tc ...
- C 语言文件拷贝
相关的方法: int fputs(const char*s,FILE *stream); int gets(char *s,int size,FILE *stream); 具体代码如下 /** *@a ...
- JS prototype 属性
String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}
- Oauth2.0协议曝漏洞 大量社交网站隐私或遭泄露
2014年是IT业界不平常的一年,XP停服.IE长老漏洞(秘狐)等等层出不穷,现在,社交网络也爆出惊天漏洞:Oauth2.0协议漏洞 继OpenSSL漏洞后,开源安全软件再曝安全漏洞.新加坡南洋理工大 ...
- Xml+Xslt测试工具
下载地址:http://download.csdn.net/detail/a497785609/5791359 说明:下载后,修改下Xslt部分的头部:<xsl:stylesheet versi ...
- Atitti. 语法树AST、后缀表达式、DAG、三地址代码
Atitti. 语法树AST.后缀表达式.DAG.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...
- jquerymobile动态添加元素之后
jquerymobile动态添加元素之后有些不能被正确渲染的解决方法:listview: 添加 jq(".detail").listview("refresh&quo ...