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. java内存相关

    (类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是具体的,占用存储空间.) 1.java是如何管理内存的 java的内存管理就是对象的分配和释放问题.(其中包括两部分) 分配:内 ...

  2. [转载]有经验的Java开发者和架构师容易犯的10个错误

    首先允许我们问一个严肃的问题?为什么Java初学者能够方便的从网上找到相对应的开发建议呢?每当我去网上搜索想要的建议的时候,我总是能发现一大堆是关于基本入门的教程.书籍以及资源.同样也发现网上到处充斥 ...

  3. Java基础—数组(转载)

    Java 语言中提供的数组是用来存储固定大小的同类型元素.其实数组就是一个容器. 创建数组 Java 中声明数组的语法有两种: dataType[] arrayRefVar; // 首选的方法 dat ...

  4. IOS JAVA PHP 安卓 通用加密方式

    PHP代码: class Aes { private $hex_iv = '00000000000000000000000000000000'; // converted JAVA byte code ...

  5. Java IO流简单使用

    Java IO流简单使用 也许是以前IO方面接触的比较少,我对于读和写的概念老是混淆. 趁着现在实习比较闲小结一下,我个人理解读和写都是针对程序,分别就是程序的输入和输出,或者叫读入写出. Java ...

  6. openCV学习——一、Mat类

    一.Mat数据类型 在以下两个场景中使用 OpenCV 时,我们必须事先知道矩阵元素的数据类型: 使用 at 方法访问数据元素的时候要指明数据类型 做数值运算的时候,比如究竟是整数除法还是浮点数除法. ...

  7. Android 6.0 Kotlin 蓝牙BLE扫描

    package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...

  8. Linux服务器内存cache清理

    发现cache中占用大量内存,无free内存可用 使用如下命令清理: syncsysctl -w vm.drop_caches=1 转自:http://blog.csdn.net/sky_qing/a ...

  9. 深入理解JVM 垃圾收集器(下)G1收集器

    1.回顾CMS 1.1堆内存结构 1.2新生代GC 1.3老年代GC 2.G1收集器 2.1G1实现概览及使用场景 G1的推荐使用场景 2.2GC 2.2.1新生代GC 2.2.2老年代GC 老年代G ...

  10. Mybatis常见问题

    1.#和$的区别和联系 1.1#是占位符,会对Sql进行预编译,相当于?:$是做Sql拼接,有sql注入的隐患 1.2#不需要关注数据类型,Mybatis自动实现类型转换,$必须自己判断数据类型联系 ...