Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

 
法I:BST的中序遍历结果是递增序列。把这个递增序列存储到数组中,再遍历一遍数组,便可知道swap的两个元素
class Solution {
public:
void recoverTree(TreeNode *root) {
vals.clear();
treeNodes.clear();
inorderTraverse(root); sort(vals.begin(), vals.end());
for (int i = ; i < treeNodes.size(); ++i)
{
treeNodes[i]->val = vals[i]; //只改变值,不改结构
}
} void inorderTraverse(TreeNode* root)
{
if (!root)
return; inorderTraverse(root->left);
vals.push_back(root->val);
treeNodes.push_back(root);
inorderTraverse(root->right);
}
private:
vector<int> vals;
vector<TreeNode*> treeNodes;
};
法II:空间复杂度O(1)的解决方案。在中序遍历的时候,当第一次扫描到前一个节点>后一个节点,那么必定是前一个节点错了;当第二次扫描到前一个节点>后一个节点,那么必定是后一个节点错了。有一种特殊情况是:第一次扫描到错误的时候,两个节点都是在错误的位置,所以在第一次扫描到错误时,也需要把后一个节点记录下来。
/**
* 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) {
TreeNode* pre = NULL;
TreeNode* swap1 = NULL;
TreeNode* swap2 = NULL;
inOrderTraverse(root,pre,swap1,swap2); int tmp = swap1->val;
swap1->val = swap2->val;
swap2->val = tmp;
} void inOrderTraverse(TreeNode* root, TreeNode* &pre,TreeNode* &swap1,TreeNode* &swap2){ //important to use &, otherwise new object will use a new address and the result won't bring back to caller //visit left child
if(root->left) inOrderTraverse(root->left,pre,swap1,swap2); //visit root
if(pre && root->val < pre->val){
swap2 = root;
if(swap1==NULL) {
swap1 = pre;
}
}
pre = root; //visit right child
if(root->right) inOrderTraverse(root->right,pre,swap1,swap2);
}
};

99. Recover Binary Search Tree (Tree; DFS)的更多相关文章

  1. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

  4. 【LeetCode】99. Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

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

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

  6. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

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

  7. leetcode 99 Recover Binary Search Tree ----- java

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

  8. 99. Recover Binary Search Tree

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  9. LeetCode OJ 99. Recover Binary Search Tree

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

随机推荐

  1. Spring Cloud 服务网关Zuul

    Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...

  2. 【转】JMeter技巧集锦

    JMeter是一个流行的用于负载测试的开源工具,具有许多有用的功能元件,如线程组(threadgroup),定时器(timer),和HTTP取样(sampler)元件.本文是对JMeter用户手册的补 ...

  3. http遇到的那些坑,iis上传文件报413错误 asp.net MVC

    话不多说,直接上解决方法. 修改配置文件  applicationHost.config  具体地址在C:\Windows\System32\inetsrv\config 按照下面的节点进行 添加&l ...

  4. 016:Explain

    一. Explain EXPLAIN 官方文档 1.explain说明 explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的 使用explain的时候,也可以使用desc 5.6 版 ...

  5. JavaScript中的可枚举属性与不可枚举属性

    在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...

  6. 走了很多弯路的CCScrollView

    最近在学习Cocos2d-x,学习CCScrollView的时候走了很多弯路,决定记录下来. 学习cocos2d-x的最大的困惑就是资料不是很齐全,网上有很多资料,但是版本差异大,其次深度低,讲解不够 ...

  7. C++何时使用引用

  8. scrapy框架的日志等级和请求参数

    一 . Scrapy的日志等级 - 在使用 scrapy crawl xxx 允许程序时,在终端里打印输出的就是scrapy的日志信息 - 日志信息的种类 : ERROR : 错误信息 WARNING ...

  9. Spring实战之切面编程

    如果要重用通用功能的话,最常见的面向对象技术是继承(inheritance)或委托(delegation).但是,如果在整个应用中都使用相同的基类,继承往往会导致一个脆弱的对象体系:而使用委托可能需要 ...

  10. 「小程序JAVA实战」小程序头像图片上传(上)(43)

    转自:https://idig8.com/2018/09/08/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan40/ 在微信小程序中 ...