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

Recover the tree without changing its structure.

Ref:http://fisherlei.blogspot.com/2012/12/leetcode-recover-binary-search-tree.html

O(1)的解法就是
Inorder traverse, keep the previous tree node,
Find first misplaced node by
if ( current.val < prev.val )
   Node first = prev;

Find second by
if ( current.val < prev.val )
   Node second = current;

After traversal, swap the values of first and second node. Only need two pointers, prev and current node. O(1) space.

但是这个解法的前提是Traverse Tree without Stack. 中序遍历如何才能不使用栈。这里就要引入一个概念, Threaded Binary Tree。So, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.

1. Initialize current as root
2. While current is not NULLIf current does not have left childa) Print current’s datab) Go to the right, i.e., current = current->rightElsea) Make current as right child of the rightmost node in current's left subtreeb) Go to this left child, i.e., current = current->left
public class Solution {public void recoverTree(TreeNode root) {TreeNode f1 = null, f2 = null;TreeNode current, pre, parent = null;if(root == null)return;// 用来记录是第几次找到bad node, 因为第一次找到的bad node为cur.pre,第二次找到bad node是curboolean found = false;current = root;while(current != null){                if(current.left == null){if(parent != null && parent.val > current.val){if(!found){f1 = parent;found = true;}f2 = current;}parent = current;current = current.right;     }   else{/* Find the inorder predecessor of current */pre = current.left;while(pre.right != null && pre.right != current)pre = pre.right;/* Make current as right child of its inorder predecessor */if(pre.right == null){pre.right = current;current = current.left;}/* Revert the changes made in if part to restore the originaltree i.e., fix the right child of predecssor */  else{// 注意跳进这里的原因是,当把root 设为左子树的最右节点时,root.left 仍然为左子树的根节点,相当于生成了一个环pre.right = null;if(parent.val > current.val){if(!found){f1 = parent;       found = true;}// f2 不写在else里的原因是 有可能是相邻的两个元素交换了f2 = current;}parent = current;current = current.right;     } /* End of if condition pre->right == NULL */} /* End of if condition current->left == NULL*/} /* End of while */if(f1 != null && f2 != null){int tmp = f1.val;f1.val = f2.val;f2.val = tmp;}}
}        

/*** Definition for binary tree* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public static void recoverTree(TreeNode root) {ArrayList<TreeNode> inorderList = new ArrayList<TreeNode>();inorder(root, inorderList);int first= -1, second  = -1; boolean secondNumber = false;for(int i = 1; i < inorderList.size();i++){if (inorderList.get(i-1).val > inorderList.get(i).val){if(!secondNumber){first = i-1;second = i;secondNumber = true;}else{second = i;}}}swap(inorderList.get(first), inorderList.get(second));}private static void inorder(TreeNode root, ArrayList<TreeNode> inorderList){if(root != null){inorder(root.left, inorderList);inorderList.add(root);inorder(root.right, inorderList);}}private static void swap(TreeNode i, TreeNode j){int tmp = i.val;i.val = j.val;j.val = tmp;}}

转载于:https://www.cnblogs.com/RazerLu/p/3557776.html

Recover Binary Search Tree相关推荐

  1. [leetcode] Recover Binary Search Tree

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

  2. LeetCode 99. Recover Binary Search Tree

    LeetCode 99. Recover Binary Search Tree 刚看到这个题真是一脸懵逼啊... 博客转载自:http://www.cnblogs.com/grandyang/p/42 ...

  3. 99. Recover Binary Search Tree (Tree; DFS)

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

  4. Recover Binary Search Tree,恢复二叉排序树

    问题描述:题意就是二叉树中有两个节点交换了,恢复结构. Two elements of a binary search tree (BST) are swapped by mistake. Recov ...

  5. LeetCode OJ - Recover Binary Search Tree

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

  6. 99. Recover Binary Search Tree(恢复二叉搜索树)

    题目链接:https://leetcode.com/problems/recover-binary-search-tree/ 思路: 看到二叉搜索树时,我想到的是中序遍历符合从大到小的原则. 然后根据 ...

  7. 99. Recover Binary Search Tree 恢复二叉搜索树

    二叉搜索树中的两个节点被错误地交换. 请在不改变其结构的情况下,恢复这棵树. 示例 1: 输入: [1,3,null,null,2] 1/3\2 输出: [3,1,null,null,2] 3/1\2 ...

  8. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

  9. 99. Recover Binary Search Tree

    一.题目 1.审题 2.分析 给出一个二叉查找树,其中有两个元素的位置弄错了,写算法将其恢复. 二.解答 1.思路: 方法一. 通过中序遍历可以确定一棵二叉查找树由小到大的顺序. 所以在此错位的查找树 ...

最新文章

  1. 2.2版本发布!TensorFlow推出开发者技能证书
  2. C++ 中内存分配和回收
  3. Linux ping 使用教程,linux ping命令的几个简单使用方法
  4. [学习笔记]模拟电路技术
  5. 可以直接考信息系统项目管理师吗
  6. SDWebImage源码阅读(九)SDWebImageDownloader
  7. 浅释一下,为什么要使用接口?
  8. 什么是:before和:after?
  9. python管道怎么使用_Python – 如何使用管道执行shell命令?
  10. 控制工程中的数学建模(4)——控制系统时域数学模型建立的一般步骤(之一)
  11. c4d打开没反应_【解决问题】C4D打开设置崩溃
  12. STM32F103 用CS1237 /HX711 芯片制作电子秤
  13. 如何将HTML与win10桌面壁纸,Win10默认桌面背景怎么设置
  14. 大数据统计分析毕业设计_数据分析毕业设计 大数据可视化毕业设计
  15. docker安装mssql
  16. 211计算机考研到985难度,普通人想考研到985/211院校到底有多难?听听他们的心声!...
  17. python计算复数的辐角,(Python 3)1051复数乘法(15分),python31051
  18. 写自己的故事,帮别人说话
  19. 用了TCP协议,就一定不会丢包吗?
  20. dialog 刘海屏、水滴屏、全面屏 全屏显示

热门文章

  1. matlab 求解黄金分割率
  2. FPGA之道(66)代码中的约束信息(三)存储器以及寄存器的相关约束
  3. 连续时间傅里叶变换的性质(简介及推导)
  4. T-Mobile旗下网站又曝安全漏洞 允许任何人查看他人账户信息
  5. 【Spark Summit East 2017】Kerberizing Spark
  6. webpack 实践笔记(一)--- 入门
  7. iOS开发之性能优化
  8. iphone 一些小游戏.
  9. 轻松恢复误删除的共享文件,DPM2007系列之六
  10. 协议学习:TCP/IP协议之物理层 上