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 ...
随机推荐
- JAVA_SE复习(basic)
一.数据类型 1.基本数据类型 Ps:有效标识符:_.字母.$开头 之后可有数字 整型:byte 1 short 2 int 4 long 8 (字节) 取值范围:其范围是从负2 的该数据类型位数 ...
- linux网络编程九:splice函数,高效的零拷贝
from:http://blog.csdn.net/jasonliuvip/article/details/22600569 linux网络编程九:splice函数,高效的零拷贝 最近在看<Li ...
- IIS上的错误与解决方案
1.未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案:在IIS上打开该网站应用程序池-->高级设置-->启动32位程序-->选True
- netty启动过程
netty先启动work线程,work线程打开selector 绑定pipeline 启动boss线程,绑定端口,注册selector,绑定op_accetp事件 ------------------ ...
- Catalyst揭秘 Day5 optimizer解析
Catalyst揭秘 Day5 optimizer解析 Optimizer是目前为止中catalyst中最重要的部分.主要作用是把analyzed logicalPlan变成optimized Log ...
- 【C++】GacLib——ListView.ViewSwitching
http://www.gaclib.net/Demos/Controls.ListView.ViewSwitching/Demo.html#FILESYSTEMINFORMATION_H
- 【学习总结】UIGestureRecognizer(手势识别器)
基本知识点 : -> IOS 3.2之后 , 苹果推出了手势识别功能 ( Gesture Recognizer ) 在触摸事件处理方面 , 简化开发难度. -> UIGesture Rec ...
- JQuery 知识点
1:animate 动画效果 $(function () { $(".sidebar-nav a").mouseover(function () { $(this).animate ...
- [转]popwindow用法
[转]弹出窗口的两种实现方式 PopupWindow 和 Activity 链接:http://www.cnblogs.com/winxiang/archive/2012/11/20/2778729 ...
- Fail-fast的原因及解决方法
[转载]:http://blog.csdn.net/chenssy/article/details/38151189 在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayL ...