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(恢复二叉搜索树)的更多相关文章

  1. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  4. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. [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 ...

  6. [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 ...

  7. 099 Recover Binary Search Tree 复原二叉搜索树

    二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树. 详见:https://leetcode.com/problems/recover-binary-search-tree/submission ...

  8. [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 ...

  9. [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 ...

  10. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. thinkphp使用阿里云OSS最新SDK,文件部署

    这文章是建立在你已经注册号阿里云的OSS,和创建好Bucket前提下: 其实阿里云的帮助与文档写的很详细,这里只说一下源码方式 1.phpsdk下载地址(摘自阿里云OSS的帮助与文档)(也有我自己下载 ...

  2. MAC brew软件安装

    之前一直怀念ubuntu下的apt-get,因为实在是方便,需要安装什么,一个命令搞定,相关的依赖包统统由apt-get维护.下载,编译,安装,那叫一个痛快.什么软件用着不爽,一个命令卸载! 怀念ap ...

  3. PHP iconv函数

    最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...

  4. 牛客网暑期ACM多校训练营(第一场)- J Different Integers (莫队)

    题意:裸的莫队题,每个查询Li,Ri,返回区间[1,Li]和[Ri,N]区间中不同的数的个数. 分析:正常的离线查询,是求区间[Li,Ri]中要求的答案,而该题所求答案为外侧两个区间中的答案,那么cn ...

  5. 开发者需要知道的iOS 12

    总体概况 iOS 12总体来看是对现有iOS的一次改进,并没有太多突破性的功能或者框架,但是Apple在底层做了很多优化的工作,优化了性能,提供了更强大的安全性,增强了AR.Siri体验,让人工智能更 ...

  6. Building an FTP Test Plan

    参考:http://jmeter.apache.org/usermanual/build-ftp-test-plan.html 1.创建一个线程组 2.线程组--->添加--->配置元件- ...

  7. APDU指令返回码及其代表含义

    9000 正常 成功执行6200 警告 信息未提供6281 警告 回送数据可能出错6282 警告 文件长度小于Le6283 警告 选中的文件无效6284 警告 FCI格式与P2指定的不符6300 警告 ...

  8. CSS3小图标菜单导航

    在线演示 本地下载

  9. 【React Native开发】React Native进行签名打包成Apk

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50525976 本文出自:[江清清的博客] (一)前言 [好消息]个人 ...

  10. React-Native 常用组件学习资料链接

    以下链接是自己开发RN工程时参考的一些不错的资料,给喜欢学习的朋友分享以下. React-Native组件用法详解之ListViewhttp://www.jianshu.com/p/1293bb8ac ...