[Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
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)空间的话可以直接中序遍历来找问题节点。
如果是O(1)空间的话,基本上就只能是原地操作了。
这里介绍一个Morris Inorder Traversal。可以实现:
1. 如果当前节点有左子树,那么找到左子树的最右节点并把它右指针指向当前节点。当前节点转移到其左节点。
2. 如果左子树的最右节点已经指向了当前节点(之前已经遍历过左子树),那么还原最右节点的右指针为null, 输出当前节点,当前节点转移到其右节点。
3. 如果当前节点没有左子树,那么直接输出当前节点并将其转移到右节点。
写成代码如下:
public void recoverTree(TreeNode root) {
TreeNode cur = root;
while(cur!=null) {
if(cur.left!=null) {
TreeNode temp = cur.left;
while(temp.right!=null && temp.right!=cur)
temp = temp.right;
if(temp.right==null) {
temp.right = cur;
cur = cur.left;
}
else {
temp.right=null;
cur=cur.right;
}
}
else {
cur=cur.right
}
}
}
}
结合这道题,只需要在print的地方添加代码即可。
只存在一处错误,遍历时可能出现两种情况:
1. 发现一次原先节点值比当前节点值大,例如:(1, 4, 3, 7, 9)这时只有对调这两个节点值即可。
2. 发现两次原先节点值比当前节点值大,例如: (1, 9, 4, 5, 3, 10)这时需要对调第一次的原先节点值和第二次的当前节点值。
使用两个TreeNode wrong1, wrong2记录这两个错误节点,如果是第一次发现原先节点比当前节点值大的错误,则wrong1置为原先节点,wrong2置为当前节点。如果又发现一次,则只更改wrong2.
注意原先节点pre和当前节点cur的关系,只有cur即将挪向右节点之前才将pre置为cur. (因为cur挪向左节点是第一次遍历左子树,仅仅用来连接,第二次遍历才输出)
完整代码如下:
public void recoverTree(TreeNode root) {
TreeNode cur = root;
TreeNode pre = null;
TreeNode wrong1 = null;
TreeNode wrong2 = null;
while(cur!=null) {
if(cur.left!=null) {
TreeNode temp = cur.left;
while(temp.right!=null && temp.right!=cur)
temp = temp.right;
if(temp.right==null) {
temp.right = cur;
cur = cur.left;
}
else {
temp.right=null;
if(pre!=null && pre.val>cur.val) {
if(wrong1==null)
wrong1=pre;
wrong2 = cur;
}
pre = cur;
cur=cur.right;
}
}
else {
if(pre!=null && pre.val>cur.val) {
if(wrong1==null)
wrong1=pre;
wrong2 = cur;
}
pre = cur;
cur=cur.right;
}
}
int t = wrong1.val;
wrong1.val = wrong2.val;
wrong2.val = t;
}
[Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)的更多相关文章
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【leetcode】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 ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Java for LeetCode 099 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 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- 第五周 Leetcode 99. Recover Binary Search Tree (HARD)
Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...
随机推荐
- Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- android中string.xml中%1$s、%1$d等的用法
今天在研究前辈写的代码的时候,突然发现string里面出现了<stringname="item_recent_photo">最近拍摄%1$s</string> ...
- SQL2008安装提示"Microsoft visual studio 2008早期之前的版本
打开注册表管理器(运行 --regedit 依次展开如下项目: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv 将devdiv项目导出来保存,倒出来之后可 ...
- iOS 9/10强制使用https访问网络,使用了第三方SDK的应用需要配置的信息
2017年01月01日起苹果将全面禁止使用http来访问网络. 网上扒了一些资源,解决方法还是有的,但是都不确定是否可以通过审核,毕竟实践才是检验真理的唯一标准. 后续如果上线成功,再来分享. 如果应 ...
- 查看oracle表中列的数据类型
一. SQLPLUS中,直接用 DESC[ribe] tablename 即可. 二.在外部应用程序调用查看ORACLE中的表结构时,只能用下面的语句代替: 1.看字段名与数据类型 select * ...
- Hibernate一对多(注解)
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- UIImagePickerControllerDelegate---ActionSheet---获得设备型号
<pre name="code" class="java">//IOS主要用的是UIImagePickerControllerDelegate这个事 ...
- laravel中日志为daily时如何设置最大保存天数
在laravel中,日志设置为daily时,默认保存七天的日志,超过则清除七天前的日志.可修改默认的设置,假如要保存30天的日志,则配置如下: 在配置文件config/app.php中添加如下代码: ...
- Android开发工具全面转向Android Studio(3)——AS project/module的目录结构(与Eclipse对比)
如果AS完全还没摸懂的,建议先看下Android开发工具全面转向Android Studio(2)——AS project/module的CRUD. 注:以下以Windows平台为标准,AS以目前最新 ...