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 ...
随机推荐
- Keras之函数式(Functional)模型
函数式(Functional)模型 我们起初将Functional一词译作泛型,想要表达该类模型能够表达任意张量映射的含义,但表达的不是很精确,在Keras2里我们将这个词改移为“函数式”,函数式模型 ...
- java内存相关
(类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是具体的,占用存储空间.) 1.java是如何管理内存的 java的内存管理就是对象的分配和释放问题.(其中包括两部分) 分配:内 ...
- Python3+Selenium3自动化测试-(五)
这里来说一说selenium中的等待方式,其实在webdriver只有两种类型等待方式,显式等待和隐式等待,之前是在程序运行过程中使用time模块中的sleep进行代码的休眠进行强制等待,是显式等待中 ...
- QT解析嵌套JSON表达式
QT5开发环境集成了解析JSON表达式的库.使用很方便. 友情提示一下,好像在QT4环境里.须要到官网下载相关的库文件才干使用解析功能.话不多说,上代码 1.在pro文件里增加 QT += scrip ...
- PHP Laravel 本地化语言支持
That`s it. 我发如今网上Laravel的学习资料实在是太少了.好多东西须要自己去弄.去理解. 我的方法另一个,就是去github上面下载老外写的Laravel站点,然后拿下来自己执行 ...
- node.js---sails项目开发(3)
1.为新建的sails数据库新建一个用户,首先连接数据库 mongo localhost:27017 (1)显示所有数据库 (2)切换数据库 show dbs use sails 新建一个用户 账 ...
- python16_day12【html、css】
一.HTML 所有HTML标签操作 <!DOCTYPE html> <html lang="en"> <head> <!--编码--> ...
- javascript DOM dindow.docunment对象
一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunment.getElementById(&qu ...
- kafka--producer配置解析
producer解析 主要是解析一下producer的相关配置以及一些使用场景 相关解析 名称 说明 类型 默认值 有效值 重要性 bootstrap.servers 用于建立与kafka集群连接 ...
- JavaScript delete用法,属性,特性,执行上下文,激活对象 综合篇
一.问题的提出 我们先来看看下面几段代码,要注意的是,以下代码不要在浏览器的开发者工具(如FireBug.Chrome Developer tool)中运行,原因后面会说明: 为什么我们可以删除对象的 ...