[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;
//print
cur=cur.right;
}
}
else {
//print
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;
//print
if(pre!=null && pre.val>cur.val) {
if(wrong1==null)
wrong1=pre;
wrong2 = cur;
}
pre = cur;
cur=cur.right;
}
}
else {
//print
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)的解法. 交换的情况只有两 ...
随机推荐
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- textFiled的placeHolder字体颜色
self.title=@"修改UITextField的placeholder字体颜色"; UITextField *textTF=[[UITextField alloc]initW ...
- 【转】三十分钟掌握STL
转自http://net.pku.edu.cn/~yhf/UsingSTL.htm 三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以 ...
- Python.with.context-manager
Context Manager 1. Context Manager简介 "Context managers are a way of allocating and releasing so ...
- lightoj 1427 - Substring Frequency (II) AC自动机
模板题,找来测代码. 注意有相同单词 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
- <停车卫> 产品需求说明书 version 2.0
<停车卫> 产品需求说明书 文档版本号: Version 2.0 文档编号: xxxx 文档密级: 归属部门/项目: 产品名: 停车卫 子系统名: 编写人: kina 编写日期: 2015 ...
- jQueryDOM操作笔记
attr(name[,value]):value(任意|函数) $('*').attr('title',function(index,previousValue){ return previousVa ...
- jquery双击事件
<html> <head><meta http-equiv="Content-Type" content="text/html; chars ...
- WPFUIElement的Background的问题
<Border Name="> <Border.Background> <VisualBrush> <VisualBrush.Visual> ...
- 开发前准备 va2015安装
1.下载vs2015 2.进行安装(同时安装node.js.npm与Android SDK,会省很多时间) 安装的时候要选择自定义安装 如果先安装了Android SDK的话就不要勾选了,我就是勾选了 ...