【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 ...
随机推荐
- hdu 4447 Yuanfang, What Do You Think?
思路: 这题有个结论也可以自己归纳: 对于给定的n,其约数用pi表示 T(n)=T(p1)T(p2)……T(pn)T(n') 其中T(n')是这个式子所独有的也就是 T(n')=(x^n-1)/T(p ...
- 【POJ3358】
题目描述: 题意: 就是给定一个a/b,求a/b的结果变成二进制之后的小数.这个小数后面会有一段循环节,只要求输出循环节开始循环的位置和循环长度. 分析: 这题我是这么想的,比如说样例中的1/5,我们 ...
- APAC Practice
2016 round A A. Googol String small && large LL a[]; int dfs(LL pos, int id, bool f) { || po ...
- Tomcat处理HTTP请求源码分析(下)
转载:http://www.infoq.com/cn/articles/zh-tomcat-http-request-2 很多开源应用服务器都是集成tomcat作为web container的,而且对 ...
- ISO9001、ISO14001、OHSAS18000什么意思
ISO9001是ISO9000族标准所包括的一组质量管理体系核心标准之一.ISO9000族标准是国际标准化组织(ISO)在1994年提出的概念,是指“由ISO/TC176(国际标准化组织质量管理和质量 ...
- JRE下的rt.jar、tools.jar
JRE下的rt.jar: 这个文件是极为重要的一个文件,rt是runtime的缩写,即运行时的意思.是java程序在运行时必不可少的文件. 里面包含了java程序员常用的包,如java.lang,ja ...
- MongoDB安装及简单实验
1.Windows下安装MongoDB http://www.mongodb.org/downloads 下载msi,下一步下一步… 安装完毕后,到安装目录的bin目录下执行mongod启动数据库服务 ...
- NLP基本任务-nltk_data文本分割
将文本分割为句子 nltk.sent_tokenize(text,language) text:需要分割的文本 language:语言种类 czech捷克语 danish丹麦语 dutch荷兰语 en ...
- Burnside引理和polay计数学习小记
在组合数学中有这样一类问题,比如用红蓝两种颜色对2*2的格子染色,旋转后相同的算作一种.有多少种不同的染色方案?我们列举出,那么一共有16种.但是我们发现,3,4,5,6是同一种,7,8,9,10是用 ...
- Check if KeyValuePair exists with LINQ's FirstOrDefault
http://stackoverflow.com/questions/793897/check-if-keyvaluepair-exists-with-linqs-firstordefault 问题: ...