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 ...
随机推荐
- @Resource或者@Autowired作用/Spring中@Autowired注解、@Resource注解的区别
@Resource或者@Autowired作用不用写set get就能注入,当然,前提是你已经开启了注解功能. spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定 ...
- 关于JSP和HTML
工作中,发现非常多同事不清楚JSP与HTML的生命周期.以至于出现"JavaScript为啥不能调用JSTL标签(或EL表达式)?"的笑话问题. 以下以流程图的方式.简单说明一下过 ...
- iloc[[i]] 和 loc[[i]] 的区别
In [2]: df Out[2]: A B 0 1.068932 -0.794307 2 -0.470056 1.192211 4 -0.284561 0.756029 6 1.037563 -0. ...
- js对象成员的访问
var obj={0:1,1:'id'};//访问对象 obj['0'];//用[]来访问,不要用.的方式访问 -----对象与字符串的转换----- node.js JS对象和JSON字符串之间的转 ...
- atom搭建markdown环境及问题
1. 搭建markdown环境 > 禁用atom自带的markdown-preview插件(功能简单) > 安装插件:markdown-preview-plus@2.4.16(在markd ...
- Vivado Logic Analyzer的使用(一)
本文基于Vivado 2014.2,通过一个简单的设计来讨论Vivado Logic Analyzer的功能. 在上一篇http://blog.chinaaet.com/detail/37242中,设 ...
- [docker]一些经常或不经常用到的镜像启动方法-一些常用的docker启动方式
一些经常或不经常用到的镜像启动方法 设置容器的TZ另一种办法 参考: https://github.com/spujadas/elk-docker/blob/master/start.sh ## ov ...
- C语言第十一回合:预处理命令的集中营
C语言第十一回合:预处理命令的集中营 [学习目标] 1. 宏定义 2. 文件包括"处理 3. 条件编译 预处理命令:能够改进程序设计的 ...
- redis源码学习_链表
redis的链表是双向链表,该链表不带头结点,具体如下: 主要总结一下adlist.c和adlist.h里面的关键结构体和函数. 链表节点结构如下: /* * 双端链表节点 */ typedef st ...
- 获取JQuery UI tabs中被选中的tabs的方法
JQuery标签事件处理实例 如果你正在使用JQuery tabs而且想从基本的功能扩展到自定义的功能,这是你最好知道如何处理JQuery的点击事件. 在这篇文章中: 1.回顾如何添加当tab被点击时 ...