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. 找工作---操作系统常考知识点总结【PB】

    1.进程是并发过程中程序的执行过程 2.进程的特征:结构特征.动态性.并发性.独立性.异步性 3.临界区指在每个进程中访问临界资源的那段代码 4,现在操作系统中申请资源的基本单位是进程,在CPU得到执 ...

  2. python操作----Memcached

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  3. python文件操作_对文件进行复制拷贝_代码实现

    要求: 1,对已经存在的文件进行复制操作 2,复制后的文件在文件名后面加上[复件] 3,文件比较大如何优化处理 #-*- coding: UTF-8 -*- #这是python 2 下面写的,用的ra ...

  4. js数组、对象、正则

    1.根据给定的条件在原有的数组上,得到所需要的新数组var a = [-1, -1, 1, 2, -2, -2, -3, -3, 3, -3];function f(s, e) {    var re ...

  5. oracle 解析json格式

    1. CREATE OR REPLACE PACKAGE PKG_ANALYSE_COMMON IS -- AUTHOR : YZCHEN -- CREATED : 2013/11/26 14:12: ...

  6. hdu1010

    #include <stdio.h>#include <string.h>#include <math.h> int n,m,t;char map[10][10]; ...

  7. 创建简单的MVC项目

    一,新建一个空的MVC项目 二,连接数据库,添加一个实体模型Model.edmx

  8. 【Sort】多种排序

    这篇文章包含了插入排序,希尔排序,堆排序,归并排序和快速排序,是前几篇文章的集合. 一共包括三个文件 sort.h sort.cpp main.cpp 1.main.cpp #include < ...

  9. laravel5.2/laravel5.3入门指南 Windows 上快速安装并运行 Laravel 5.x

    1 首先要搭建本地服务器环境推荐phpstudy2016及wampServer3.0.6 下载链接可参考 http://www.cnblogs.com/zzcit/p/5823742.html 注意一 ...

  10. LYNC2013介绍和基础架构准备角色

    LYNC2013部署系列PART1:LYNC2013介绍和基础架构准备 前言:LYNC 2013发布已经很久了,本人一直在进行相关的学习和测试,在有限的资源条件下,把能够模拟出来的角色进行了安装部署, ...