99. Recover Binary Search Tree
题目:
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?
链接: http://leetcode.com/problems/recover-binary-search-tree/
题解:
恢复BST。和上一题validate BST一样,可以使用recursive的 in-order traversal。 先递归查找左子树里root.val < prevNode.val的,确定第一个逆序的节点,之后继续搜索,查找第二个逆序的节点。注意不可以在第一次找到第二个逆序的时候就返回了,要继续查找。否则像[2, 3, 1]这样的case会通不过,因为直接就把2和3 swap了,并没有找到1。 最后把两个节点的val交换一下就可以了。
Time Complexity - O(n), Space Complexity - O(1)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode firstNode;
TreeNode secondNode;
TreeNode lastNode; public void recoverTree(TreeNode root) {
this.lastNode = new TreeNode(Integer.MIN_VALUE);
inorderTraversal(root);
int tmp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = tmp;
} private void inorderTraversal(TreeNode root) {
if(root == null)
return;
inorderTraversal(root.left);
if(firstNode == null && this.lastNode.val >= root.val)
firstNode = lastNode;
if(firstNode != null && this.lastNode.val >= root.val)
secondNode = root;
this.lastNode = root;
inorderTraversal(root.right);
}
}
Update:
想了一下,不用初始化lastNode = new TreeNode(Integer.MIN_VALUE)。 当然初始化也可以,it doesn't matter。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode firstNode;
TreeNode secondNode;
TreeNode lastNode; public void recoverTree(TreeNode root) {
inorderTraversal(root);
int tmp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = tmp;
} private void inorderTraversal(TreeNode root) {
if(root == null)
return;
inorderTraversal(root.left);
if(lastNode != null && firstNode == null && lastNode.val >= root.val)
firstNode = lastNode;
if(lastNode != null && firstNode != null && lastNode.val >= root.val)
secondNode = root;
lastNode = root;
inorderTraversal(root.right);
}
}
Reference:
https://leetcode.com/discuss/13034/no-fancy-algorithm-just-simple-and-powerful-order-traversal
https://leetcode.com/discuss/7319/an-elegent-time-complexity-and-space-complexity-algorithm
https://leetcode.com/discuss/41182/tree-deserializer-and-visualizer-for-python
https://leetcode.com/discuss/31543/a-concise-java-solution-using-morris-inorder-o-time-o-1-space
https://leetcode.com/discuss/26310/detail-explain-about-morris-traversal-finds-incorrect-pointer
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html
99. Recover Binary Search Tree的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode OJ 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【一天一道LeetCode】#99. Recover Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- a2x
#include <typeinfo> template <typename T> bool a2x( T& _f , char* p) { if( !p ) retu ...
- 使用DNSSCrypt解决DNS污染问题
本文转自 月光博客,如有需要,请阅读原文. google近期在国内是不能访问了,dropbox这货居然也被DNS污染了,幸好发现DNSCrypt这一神器,防止DNS污染的绝佳工具. 基本原理:DNSC ...
- myeclipse与数据库进行连接(无需写代码进行验证)
首先对SqlServer配置管理器进行设置. 1.打开SqlServer配置管理器 2.进入SQL配置管理器后,选中左侧“SQL Server网络配置”>>再选中“MSSQLSERVER的 ...
- 多文件上传 iOS功能
多文件上传 iOS功能,原文来自ios教程网整理的,大家可以看看演示:ios.662p.com ,喜欢的朋友可以看看我的博客吧. NSURL* url = [NSURL URLWithString:@ ...
- HTTP Error 500.21解决方案
Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决方案 今天在测试网站的时候,在浏览器中输入http://localhost/时 ...
- winForm 打印预览
自己很少写技术博客,虽然已经干程序员两年多了,winform开发,web开发都干过,不论项目大小对于.net的相关技术也是了解的,如mvc,wcf,wpf,silverlight,socekt通讯,n ...
- centos 安装ecshop出现date错误
centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...
- 快速搭建Web环境 Angularjs + Express3 + Bootstrap3
快速搭建Web环境 Angularjs + Express3 + Bootstrap3 AngularJS体验式编程系列文章, 将介绍如何用angularjs构建一个强大的web前端系统.angula ...
- Oracle 动态视图1 V$LOCK
v$lock显示数据库当前持有锁情况 Column Datatype Description SID NUMBER 会话ID TYPE VARCHAR2(2) 表示锁的类型.值包括TM,TX,等 ID ...
- ajax中的post方法中回调函数不执行的问题
前一段时间接触了JQuery Ajax中的.post()方法和.get()方法,感觉到ajax的简洁和强大,当用到.post()方法时,去W3上查找相关的使用方法,感觉十分简单,用法很明了,然后,直接 ...