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. PHP使用Mongodb

    一.安装Mongodb的PHP扩展 wget http://pecl.php.net/get/mongo-1.2.7.tgz //下载扩展包tar zxvf mongo-1.2.7.tgzcd mon ...

  2. 注册tomcat为windows服务(转载)

    第一部分 应用场景 需要服务器上Tomcat不显示启动窗口 需要服务器上Tomcat开机自启动 ... 第二部分 配置过程 一.修改配置文件 1 {Tomcat_HOME}/bin/service.b ...

  3. dplyr快速入门

    RStudio Blog 介绍dplyr 包已发布 (Introducing dplyr), 此包将原本 plyr 包中的 ddply() 等函数进一步分离强化, 专注接受dataframe对象, 大 ...

  4. 17届计算机应届生秋季校招分享 to Tomorrow

    首先自我介绍一下,本人来自普通二本院校,计算机科学与技术专业,在校有一到两年asp.net项目经验,花了两个星期左右的时间转向java.现将此次的求职经历,分为三阶段,分享给大家. First Sta ...

  5. mysql-xtrabackup

    使用xtrabackup进行MySQL数据库备份 2013年10月04日 ⁄ MySQL ⁄ 共 11306字 ⁄ 使用xtrabackup进行MySQL数据库备份已关闭评论 ⁄ 被围观 34,116 ...

  6. oradebug工具使用3(转载)

    1 oradebug介绍 oradebug主要是给oracle支持人员使用的,尽管很早便有,但oracle官网很少有记载.他是个sql*plus命令行工具,有sysdba的权限就可以登入,无需特别设置 ...

  7. [转]C#读写远程共享文件夹

    1.在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.200.8.73,共享文件夹名字是share,访问权限,用户名是administrator,密码是11111111. 2.新建一个控制台 ...

  8. springmvc国际化资源文件

    spring配置文件中添加 <!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springf ...

  9. Java应用程序连接数据库--JDBC基础

    Java应用程序连接数据库--JDBC基础   Java应用程序连接数据库–JDBC基础 <!-- MySQL驱动,连接数据库用,由数据库厂商提供 --> <dependency&g ...

  10. SpringMVC学习大纲

    PartA: 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) SpringMVC ...