Recover Binary Search Tree-恢复二叉查找树
题目描述:
由于某种原因一个二叉排序树的两个节点的元素被交换,在不改变树的结构的情况下恢复这颗二叉排序树
题目来源:
http://oj.leetcode.com/problems/recover-binary-search-tree/
题目分析:
中序遍历二叉排序树会得到递增序列,如果两个元素交换,递增序列的顺序必被破坏,例如:1, 2, 3, 4, 5, 6, 7。交换后可能会有
(1)1, 2, , 4, 5, , 7 (2)1, 2, 3, 4, , ,7 等
在中序遍历时,出现当前元素比中序遍历中前一个元素小时,找到出错元素,维护一个刚刚遍历的节点的指针pre,当遍历结束当前节点时,更新pre指针
时间复杂度:O(1)
- 示例代码:
TreeNode *pre, *first, *second; void recoverTree(TreeNode *root) {
pre = first = second = NULL;
inOrder(root);
int tmp = second->val;
second->val = first->val;
first->val = tmp;
} void inOrder(TreeNode*& root) {
if(root == NULL)
return ; inOrder(root->left);
if(pre != NULL && pre->val > root->val) {
first == NULL ? first = pre, second = root : second = root;
}
pre = root;
inOrder(root->right); return ;
}
Recover Binary Search Tree-恢复二叉查找树的更多相关文章
- [leetcode]99. 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 笔记 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
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- 【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 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [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,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- C++中栈的出栈,入栈规则:A,B,C,D,E
考题: 栈底至栈顶一次存放元素 ABCD 在第五个元素E入栈之前 栈中元素可以出栈,则出栈序列可能是_____a d___________. a. ABCED b. DBCEA c. CD ...
- android继承Dialog实现自定义对话框
有时需要自定义对话框,可以使用AlterDialog.Bulider,比如下面的代码片段 new AlertDialog.Builder(self) .setTitle("标题") ...
- 格式化输出[parts/iomanip]
/* 用ios类中的成员函数来进行IO格式的控制总需要写一条单独的语句,而不能直接嵌入到IO语句中,显得很不方便,因此C++又提供了一种用操作符来控制IO的格式.操作符分为带参和不带参两种,带参的定义 ...
- CRT团队组员博客地址统计
CRT团队GitHub地址:https://github.com/CoffeeRobotTeam/Coffee-Robot-System 洪超 http://www.cnblogs.com/chaoh ...
- [转载]关于android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错
原文地址为:http://blog.csdn.net/springsky_/article/details/7442388 因为入行移动测试,所以很多测试环境的搭建.从中遇到了和这个GG同样的问题.怕 ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- QGraphics
QGraphicsView和QGraphicsScene QGraphicsScene提供一个场景,可以添加各种item,QGraphicsView用于将元素显示,并支持旋转和缩放:可以将QGraph ...
- 寒假222_codeforces 290 div 2 D
序号5: 想了很久的DP ,应该很简单,但是.. 题目直接转化为求n个数中选一些数GCD=1且花费最小 数比较大 map HASH 还有一点 我们知道 GCD(X,X*Y)==X; 所以我的代码里不 ...
- sublime 配置C++
1. 安装C语言编译器MinGW,并把MinGW安装目录下的bin目录添加到环境变量PATH里.详细方法参照MinGW安装和使用 2. 在SublimeText安装目录下的Data\Packages\ ...
- Ajax HTML, JS
Ajax Request HTML <script></script>及外部的js文件,都需要 var scriptStrs = response.match(/\<[\ ...