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?

=============

解法:来自leetcode 150题集

O(n)的解法,开一个指针数组,中序遍历,将节点指针一次存放在数组中,

然后寻找两处逆向的位置,先从前往后找第一个逆序的位置,然后从后往前寻找第二个逆序的位置,交换两个指针的值,

递归/非递归中序遍历一般需要用到栈,空间也是O(n)的,可以利用morris中序遍历的方式

=====

code:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. ///
  13. void recoverTree(TreeNode *root){
  14. pair<TreeNode *,TreeNode *> broken;
  15. TreeNode *curr = root;
  16. TreeNode *prev = nullptr;
  17. broken.first = broken.second = nullptr;
  18.  
  19. while(curr!=nullptr){
  20. if(curr->left==nullptr){
  21. detect(broken,prev,curr);
  22. prev = curr;
  23. curr = curr->right;
  24. }else{
  25. auto node = curr->left;
  26. ///prev = curr->left;
  27. while(node->right != nullptr && node->right!=curr){
  28. node = node->right;
  29. }
  30.  
  31. ///find predecessor
  32. if(node->right==nullptr){
  33. node->right = curr;
  34. curr = curr->left;
  35. }else{
  36. node->right = nullptr;
  37. detect(broken,prev,curr);
  38. prev = curr;
  39. curr = curr->right;
  40. }
  41. }///if-else
  42. }///while
  43.  
  44. swap(broken.first->val,broken.second->val);
  45. }
  46. void detect(pair<TreeNode *,TreeNode *> &broken,TreeNode *prev,
  47. TreeNode *curr){
  48. if(prev!=nullptr && prev->val > curr->val){
  49. if(broken.first == nullptr) broken.first = prev;
  50. broken.second = curr;
  51. }
  52. }
  53. };

99. Recover Binary Search Tre的更多相关文章

  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 ----- java

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

  7. 99. Recover Binary Search Tree

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

  8. LeetCode OJ 99. Recover Binary Search Tree

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

  9. 【一天一道LeetCode】#99. Recover Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...

随机推荐

  1. 让Grub 2来拯救你的 bootloader

    没有什么事情比 bootloader 坏掉更气人的了,充分发挥 Grub 2 的作用,让 bootloader 安分工作吧.为什么这么说? Grub 2 是最受欢迎的 bootloader ,几乎用在 ...

  2. 83. Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. Sprint第二个冲刺(第六天)

    一.Sprint 计划会议: 因为这两天课程较多的原因,表面上进度可能可能没有太大的变化,其实组员们都有完善之前做的功能,正在做的功能也在抓紧时间完成.俗话说得好,慢工出细活 ,不能因为赶进度就随便做 ...

  4. xctest错误问题解决

    xctest xctest.h file not found(null): Framework not found XCTest 在FrameWork Search Path里增加以下内容$(PLAT ...

  5. HTML 常见代码整合;

    html+css代码 文本设置 1.font-size: 字号参数 2.font-style: 字体格式 3.font-weight: 字体粗细 4.颜色属性 color: 参数 注意使用网页安全色 ...

  6. android解析json包(接口)

    package com.http.test; 02    03    04 import org.apache.http.HttpResponse; 05 import org.apache.http ...

  7. poj2912 带权并查集

    题意:多个人玩石头剪刀布,每个人提前选定了自己出哪个手势,而其中有一种特殊的人他可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是特殊的,可以从第几局游戏中判断出来. 首先按照食 ...

  8. 越狱Season 1-Episode 8: The Old Head

    Season 1, Episode 8: The Old Head -Michael: 17 days from now they strap my brother to an electric ch ...

  9. click 绑定(二)带参数的click 事件绑定

    注1:传参数给你的click 句柄 最简单的办法是传一个function包装的匿名函数: <button data-bind="click: function() { viewMode ...

  10. php 代码大全

    1.子类访问父类静态方法 <?php class A{ static function loadById(){ $class_name = get_called_class(); $model ...