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?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

Subscribe to see which companies asked this question

先求出树的中序序列,然后在序列中寻找出错的位置。代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void recoverTree(TreeNode root) {
List<TreeNode> inorder = new ArrayList<TreeNode>();
inOrder(root, inorder); //求中序序列 TreeNode wrong1 = null;
TreeNode wrong2 = null; for(int i = 0; i < inorder.size() - 1; i++){
if(inorder.get(i).val > inorder.get(i+1).val){
if(wrong1 == null){
wrong1 = inorder.get(i);
wrong2 = inorder.get(i+1);
}
else{
wrong2 = inorder.get(i+1);
break;
}
}
}
if(wrong1 != null && wrong2 != null){
int temp = wrong1.val;
wrong1.val = wrong2.val;
wrong2.val = temp;
}
} public void inOrder(TreeNode root, List<TreeNode> inorder){
if(root == null) return;
if(root.left != null) inOrder(root.left, inorder);
inorder.add(root);
if(root.right != null) inOrder(root.right, inorder);
}
}

LeetCode OJ 99. Recover Binary Search Tree的更多相关文章

  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

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

  4. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

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

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

  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 OJ:Recover Binary Search Tree(恢复二叉搜索树)

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

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

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

  9. 【LeetCode练习题】Recover Binary Search Tree

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

随机推荐

  1. Linux之top

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  2. linux(centos7)下SVN服务器如何搭建

    linux(centos)下SVN服务器如何搭建?说到SVN服务器,想必大家都知道,可以是在LINUX下如何搭建SVN服务器呢?那么今天给大家分享一下linux(centos)搭建SVN服务器的思路! ...

  3. js数组对象方法

  4. UIView 和 CALayer 的区别和联系

    UIView是在/System/Library/Frameworks/UIKit.framework定义,也就是处于Cocoa Touch层. CALyer是在/System/Library/Fram ...

  5. linux 安装mysql数据库

    Ubuntu上安装MySQL非常简单,只需要打开终端,几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql- ...

  6. iscroll的下拉刷新,上拉翻页。

    首先对iscroll的scrollTo方法进行稍微修改如下图: 对iscroll滑动到屏幕边缘不能弹回的bug进行修复,如下代码: function scrollbug() { var self = ...

  7. linux下svn目录管理

    linux 下安装的svn目录文件: /data/svndata/winne/conf/passwd 是配置用户密码的文件路径

  8. HDU 5784 How Many Triangles

    计算几何,极角排序,双指针,二分. 直接找锐角三角形的个数不好找,可以通过反面来求解. 首先,$n$个点最多能组成三角形个数有$C_n^3$个,但是这之中还包括了直角三角形,钝角三角形,平角三角形,我 ...

  9. eclipse中git插件配置 编辑

    一.Eclipse上安装GIT插件EGit EGit插件地址:http://download.eclipse.org/egit/updates OK,随后连续下一步默认安装就可以,安装后进行重启Ecl ...

  10. struts2防止重复提交的标签

    struts2 token 使用说明 --------------------------------------------------------------------------------- ...