LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
首先是O(N)空间的方法,用递归:
/**
* 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:
void recoverTree(TreeNode* root) {
vt.clear();
vi.clear();
if(!root) return;
tranverse(root);
sort(vi.begin(), vi.end());
for_each(vt.begin(), vt.end(), [this](TreeNode * t){static int i = ; t->val = vi[i++];});
} void tranverse(TreeNode * root)
{
if(!root) return;
tranverse(root->left);
vt.push_back(root);
vi.push_back(root->val);
tranverse(root->right);
}
private:
vector<TreeNode *> vt;
vector<int> vi;
};
下面这个方法是看别人实现的,想法很好,维护两个指针,一个指向前面一个违反条件的,一个指向后面一个,q不断的进行更新,代码如下:
class Solution {
public:
void recoverTree(TreeNode* root)
{
p = q = prev = NULL;
if(!root) return;
tranverse(root);
swap(p->val, q->val);
}
void tranverse(TreeNode * root)
{
if(!root) return ;
tranverse(root->left);
if(prev && (prev->val > root->val)){
if(!p) p = prev;//这里应该注意
q = root;//注意为什么q取的是root
}
prev = root;
tranverse(root->right);
}
private:
TreeNode * p, * q;
TreeNode * prev;
};
LeetCode OJ: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 复原二叉搜索树
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] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 099 Recover Binary Search Tree 复原二叉搜索树
二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树. 详见:https://leetcode.com/problems/recover-binary-search-tree/submission ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- ThreadLocal 示例
ThreadLocal, 从字面意思上看是本地线程. 但实际上它是一个线程本地变量.它的功能就是为每一个使用该变量的线程都提供一个变量值的副本, 从而使得不会与其他线程的副本冲突. 与使用synchr ...
- tornado下的跨站请求伪造(防护)
跨站请求伪造(防护) 任何Web应用所面临的一个主要安全漏洞是跨站请求伪造,通常被简写为CSRF或XSRF,发音为"sea surf".这个漏洞利用了浏览器的一个允许恶意攻击者在受 ...
- Django 之基础学习
阅读目录 配置 视图层之路由系统配置 模版层 模版过滤器 request & response Ajax Cookie Session 分页 文件传输 Django MTV模型 Django ...
- redhat 9.0安装完不能上网解决办法(补) - 并附上redhat9.0 下载链接
昨天一位网友Q我,说我的开发环境搭建教程按步骤最后上不了网怎么解决我才突然想起9.0版本在VM7,8中存在问题,于是今天我就简单说下解决的方法. 由于本人习惯使用redhat 9.0版本所以到现在还是 ...
- 表单向controller传值如果没填controller取到的是null
jsp前端表单,向controller传数据,如果没有值,后台传入的是null,比如checkbox未选中,后台设置的Integer[] ids,接收到的ids=null,hidden标签如果没有值, ...
- <context:annotation-config/> 的理解
转载:http://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Sprin ...
- 【工具】Notepad++ 上,代码格式化工具
一.概述 Windows 自带的记事本功能太过简单,因此我常常使用 Notepad++ 查看文本.Notepad++ 支持插件功能,最近需要使用 Notepad++ 查看 Html 代码,而这些代码多 ...
- Ubuntu更新Hostname和hosts
一.概述 Hostname 即主机名,一般存放在 /etc/hostname 中.hosts 即本地域名解析文件,存放在 /etc/hosts 中. 二.测试 2.1 hostname 2.2 hos ...
- 20145219 《Java程序设计》第03周学习总结
20145219 <Java程序设计>第03周学习总结 教材学习内容总结 基本类型和类类型 基本类型:第三章中讲述的那几种,short.long.int.byte.double.char. ...
- for_each用法
for_each()是个function template #include <algorithm>头文件说明 template<class _InIt, class _Fn1> ...