leetcode 99 Recover Binary Search Tree ----- 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?
给定一个排序二叉树,然后任意交换了其中两个节点,要求在使用常数个空间和不改变结构的前提下恢复原来的二叉树。
如果用O(n)的空间复杂度,那么直接中序遍历就好了,现在用常数个,那么只需要记录两次就好了,中序遍历的时候,需要动动脑子。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode pre;
TreeNode first;
TreeNode second; public void inorder(TreeNode root){
if(root == null){
return;
}
inorder(root.left);
if(pre == null){
pre = root;
}else{
if(pre.val > root.val){
if(first == null){
first = pre;
}
second = root;
}
pre = root;
}
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;
}
} }
leetcode 99 Recover Binary Search Tree ----- java的更多相关文章
- [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 复原二叉搜索树
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#99 Recover Binary Search Tree
原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...
- 第五周 Leetcode 99. Recover Binary Search Tree (HARD)
Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...
- 【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
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- 哪些字符需要urlencode编码?具体怎么处理?
哪些字符需要urlencode编码?具体怎么处理? JS用escape()/encodeURI()/encodeURIComponent()方法编码,用unescape()/decodeURI()/e ...
- fqrouter让安卓手机登陆facebook成为可能
大多数人向来都是在电脑上通过各种代理工具来访问一些国外网站,例如facebook,twitter,然而你是否想过可以通过你的手机来畅游这些网站呢,接下来我将介绍一种通过fqrouer实现使用安卓手机畅 ...
- HDU 1465
排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常 ...
- KMP算法中next函数的理解
首先要感谢http://blog.csdn.net/v_july_v/article/details/7041827以及http://blog.chinaunix.net/uid-27164517-i ...
- 分享第一次使用ProcessOn心得
最近朋友推荐了我一款在线作图工具ProcessOn,感受使用了几天感觉确实很不错,在这里给大家分享一下! ProcessOn应该算的上是第一款完全免费在线作图工具,之前用过国外有类似的,不过都是付费的 ...
- 笔记本安装Win2012R2 心得(包含无线网卡+有线网卡驱动解决方法)
笔记本:联想昭阳E47G 无线网卡安装方法: 系统安装完毕后将自动识别无线网卡驱动,但需要手动允许WLAN服务开启.(建议,如果是拿来办公或者家用,可以安装上桌面体验)不然,QQ发的截图双击都看不起. ...
- 多比Web 3D展示(3D机房/3D监控)中间件多比Web 3D展示(3D机房/3D监控)中间件免费下载购买地址
多比3D是实现3D场景搭建的软件开发包,可以创建广泛的3D应用,适用于高端制造.能源.国防军工.教育科研.城市规划及建筑环艺.生物医学等领域的虚拟仿真,应用于虚拟展示.虚拟设计.方案评审.虚拟装配.虚 ...
- FastReport产品介绍及免费下载地址
公司地址: 俄罗斯 公司网址: http://www.fast-report.com 详细信息: 由技术总监Alexander Tzyganenko创建于1998年,Fast Reports, Inc ...
- GSM短信侦听的便宜方案
侦听GSM短信常用的是OsmocomBB + C118方案,主要是用luca/gsmmap分支.使用ccch_scan这个程序可以把通信封装成GSMTAP发给本机,然后用WireShark接收GSMT ...
- UE正则表达式查找和替换(将【,;】)替换为换行