LeetCode 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?二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树。
最简单的办法,中序遍历二叉树生成序列,然后对序列中排序错误的进行调整。最后再进行一次赋值操作。
但这种方法要求n的空间复杂度,题目中要求空间复杂度为常数,所以需要换一种方法。
递归中序遍历二叉树,设置一个pre指针,记录当前节点中序遍历时的前节点,如果当前节点大于pre节点的值,说明需要调整次序。
有一个技巧是如果遍历整个序列过程中只出现了一次次序错误,说明就是这两个相邻节点需要被交换。如果出现了两次次序错误,那就需要交换这两个节点,这两个节点一个是前次错误的pre,一个的后次错误的root。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void recoverTree(TreeNode *root) {
if(root==NULL) return;
pre=NULL;
mistake1=NULL;
mistake2=NULL;
recover(root);
if(mistake1!=NULL&&mistake2!=NULL){
swap(mistake1->val,mistake2->val);
}
}
void recover(TreeNode *root){
if(root==NULL) return;
if(root->left!=NULL) recover(root->left);
if(pre!=NULL&&pre->val>root->val){
if(mistake1==NULL){
mistake1=pre;
mistake2=root;
}else{
mistake2=root;
}
}
pre=root;
if(root->right!=NULL) recover(root->right);
}
TreeNode *pre;
TreeNode *mistake1,*mistake2;
};
LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误的更多相关文章
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...
- Leetcode700.Search in a Binary Search Tree二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. class Solution { public: ...
- LeetCode: 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 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- LeetCode: Recover Binary Search Tree [099]
[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without cha ...
随机推荐
- 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作
最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...
- BZOJ-1043 [HAOI2008]下落的圆盘
几何题... 先把所有圆储存起来,然后对于每个圆我们求得之后放下的圆挡住了的部分,求个并集,并把没被挡到的周长加进答案. #include <cstdlib> #include <c ...
- 自制wifi信号放大器
自制wifi信号放大器 只要家里安装了一台无线路由器,在家里的任何地方都可以使用带上网功能的电子产品上网,但是由于距离的问题,WiFi信号有强弱之分,离无线路由器稍微远点,信号就有所降低,上网速度受影 ...
- 刷题总结——次小生成树(bzoj1977 最小生成树+倍增)
题目: Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C ...
- android在JNI_OnLoad入口函数下断点动态调试so库
一般来说,很多APK的校验代码,都会在程序运行的时候自动加载一些动态so库,然后执行这些库中的校验代码.所以为了能够通过程序的校验,我们必须在执行这些函数之前下断点——理想的方法就是在JNI_OnLo ...
- 更改MVC3默认错误提示信息 模型验证 validation
原文发布时间为:2011-07-20 -- 来源于本人的百度文章 [由搬家工具导入] Model Metadata and Validation Localization using Conventi ...
- RowFilter 对于已获取到的dataset进行过滤
原文发布时间为:2009-11-12 -- 来源于本人的百度文章 [由搬家工具导入] DataView的属性RowFilter使用方法 p.s. 重点在于DataView是DateTable相关联的一 ...
- c#中使用事务
原文发布时间为:2009-04-14 -- 来源于本人的百度文章 [由搬家工具导入] 问:为什么要用事务? 答:事务保证要么一组操作执行成功,要么全不执行。。。。 /// <summary> ...
- spring一些总结
Spring中三种实例化bean的方法: 1)使用类构造器 <bean id="orderService" class="cn.itcast.OrderServ ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---23
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: