Recover Binary Search Tree leetcode java
题目:
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?
题解:
解决方法是利用中序遍历找顺序不对的两个点,最后swap一下就好。
因为这中间的错误是两个点进行了交换,所以就是大的跑前面来了,小的跑后面去了。
所以在中序便利时,遇见的第一个顺序为抵减的两个node,大的那个肯定就是要被recovery的其中之一,要记录。
另外一个,要遍历完整棵树,记录最后一个逆序的node。
简单而言,第一个逆序点要记录,最后一个逆序点要记录,最后swap一下。
因为Inorder用了递归来解决,所以为了能存储这两个逆序点,这里用了全局变量,用其他引用型遍历解决也可以。
代码如下:
1 public class Solution {
2 TreeNode pre;
3 TreeNode first;
4 TreeNode second;
5
6 public void inorder(TreeNode root){
7 if(root == null)
8 return;
9
inorder(root.left);
if(pre == null){
pre = root; //pre指针初始
}else{
if(pre.val > root.val){
if(first == null){
first = pre;//第一个逆序点
}
second = root; //不断寻找最后一个逆序点
}
pre = root; //pre指针每次后移一位
}
inorder(root.right);
}
public void recoverTree(TreeNode root) {
pre = null;
first = null;
second = null;
inorder(root);
if(first!=null && second!=null){
int tmp = first.val;
first.val = second.val;
second.val = tmp;
}
}
Recover Binary Search Tree leetcode java的更多相关文章
- Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Convert Sorted Array to Binary Search Tree leetcode java
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Convert Sorted List to Binary Search Tree leetcode java
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- [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 | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- WorldFinal11 (2/11)
WorldFinal 11 Trash Removal 题意 给你一个多边形,问这个多边形至少需要多宽的长度,才能把这个多边形放进去. 数据范围100 题解 数据范围只有100,暴力枚举两点,然后算最 ...
- 2016 Multi-University Training Contest 6 题解
我只能说: A Boring Question 下面公式重复了一行 \[ \sum\_{0\leq k\_{1},k\_{2},\cdots k\_{m}\leq n}\prod\_{1\leq j& ...
- Educational Codeforces Round 14 A. Fashion in Berland 水题
A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- 使用清华源和阿里源替代Ubuntu源
sudo nano /etc/apt/source.list 替换为如下文本 # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors. ...
- 在阿里云里申请免费Https证书SSL
在阿里云控制台:安全(云盾)->证书服务->购买证书里(地址:https://common-buy.aliyun.com/?spm=5176.2020520163.cas.1.zTLyhO ...
- 解决Mac OS下安装MyEclipse报错:Your system does not have sufficient memory to support MyEclipse
最近想尝尝鲜,FQ去www.myeclipseide.com上下载了最新版的MyEclipse 15CI版,安装的时候,报告如下错误(MyEclipse 14也会出现这个问题): Your syste ...
- Android 应用开发特色
Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序.1. 四大组件Android 系统四大组件分别是活动(Activity).服务(Service).广播接收器(Broadcast ...
- 5日均线MACD
1.5日均线: 5日均线是股市术语,就是股票5天的成交价格或指数的平均值,所对应的是股价的5日均线和指数的5日均线(5MA).均线指标实际上是移动平均线指标的简称. 一般在K 线图中会有3 条或4 条 ...
- C#编程(三十七)----------结构比较
结构比较 数组和元组都实现接口IStructuralEquatable和IStructuralComparable.这两个接口不仅可以比较引用,还可以比较内容.这些接口都是显示实现的,所以在使用时需要 ...