【LeetCode 99】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
题意:
一颗二叉搜索树中有2个结点的元素被误换了,要求恢复二叉搜索树的原状。
思路:
中序遍历BST,若发现一次逆序,说明是两个相连结点的误换,直接交换结点的值;若发现两次逆序,则为不相连的两个结点误换,交换错误的结点即可。用一个变量保存先前结点的状态,若发生逆序则记录,空间复杂度为常量。
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: vector<TreeNode* > errs;
TreeNode *preNode; void rec(TreeNode *root)
{
if(root->left != )
rec(root->left); if(preNode != && root->val < preNode->val)
{
errs.push_back(preNode);
errs.push_back(root);
} preNode = root; if(root->right != )
rec(root->right);
} void recoverTree(TreeNode* root) {
if(root == )
return ; preNode = ; rec(root); int temp = , index = ; if(errs.size() == )
index = ; temp = errs[]->val;
errs[]->val = errs[index]->val;
errs[index]->val = temp;
}
};
【LeetCode 99】Recover Binary Search Tree的更多相关文章
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【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(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- LeetCode OJ: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 [Tree]
Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straigh ...
随机推荐
- nmap使用详解
nmap是一个网络探测和安全扫描程序, 系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect(). ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- *[codility]MissingInteger
今天开始刷刷codility上respectable的题目,难度适中. https://codility.com/demo/take-sample-test/missing_integer 本题是找出 ...
- POJ1248 Safecracker
第一次写DFS的程序,虽然是个水题.1. 学了memset2. 可以存下来A-Z的各个次方的结果3. 可以排序优化4. 我用了t[0]==0来判断是否有解,也可设个flag5. 用了递归,也可用五层循 ...
- Ado.Net小练习03(省市联动)
前台界面: 后台代码: namespace _04省市联动 { public partial class Form1 : Form { public ...
- 微信开发之——Php批量生成带参数的二维码
带参数的二维码对于渠道营销推广来说是很有用的,可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送,可喜的是微信开通了这个接口,那下面就来研究一下吧. 具体接口说明请参见,微信公众平 ...
- Intellij IDEA 快速创建Spring Web 项目
相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...
- Win8-64位安装OpenSSL详细过程
相关软件: 1.ActivePerl 5.22.1 : http://www.activestate.com/activeperl/downloads 2.Microsoft visual_studi ...
- 关于java -version版本问题
因为安装了Oracle,而Oracel会自带JDK,安装完成后,会自动把自己的JDK设置在最前面(path变量里). 这就是为什么结果与事实不相同的原因. 解决方法: 进入系统环境变量,找到path变 ...
- Android Material Design-TabLayout的使用
TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...