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. 360度角转AS3角度

    var ang:Number=400; ang=ang%360; //as3角指0~180,0~-180的角 //1. 360度角转as3角 if(ang>180)ang-=360; else ...

  2. js监听事件 上滑消失下滑出现的效果 触摸与手势事件

    https://www.w3cmm.com/javascript/touch.html //触摸与手势事件连接tinyscrollbar //方法1var _this = $('#fabu');var ...

  3. HDU2124 Repair the Wall(贪心)

    Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...

  4. YII学习第二十三天,accessRules用法

    访问控制过滤器(Access Control Filter)访问控制过滤器是检查当前用户是否能执行访问的controller action的初步授权模式. 这种授权模式基于用户名,客户IP地址和访问类 ...

  5. dubbo 的monitor监视器安装问题——————monitor一直处于正在启动状态

    一台服务器安装完zookeeper并启动后,然后在另一服务器安装monitor     dubbo-monitor-simple-2.8.3  解压安装 修改配置文件 dubbo.container= ...

  6. haskell类型

    一.源文件 介绍这个主要是因为下文很多代码写在源文件中,然后从ghci加载源文件进行测试. 创建一个文本文件,在其中输入,并保存为add.hs文件 -- file: add.hs add x y = ...

  7. 调用短信接口,先var_dump()看数据类型是object需要json_decode(json_encode( $resp),true)转换成array

    返回的数据.先看类型,如果是object类型 先json_encode, 再json_decode,加true 转换成数组 $resp = $c->execute($req); var_dump ...

  8. Java8 (1)

    参考资料: <Java8 in Action> Raoul-Gabriel Urma 一.jdk8 客观的说,Java8是一次有重大演进的版本,甚至很多人认为java8所做的改变,在许多方 ...

  9. jQuery 学习小结

    1,jQuery是一个简单的JavaScript库,提供了一系列辅助函数:以下简称jq; 使用jq时,通常将jq代码放到head部分的事件处理方法中,也可以将其单独写出 .js 文件,引入:但无论哪种 ...

  10. digitalocean开通德国法兰克福机房,40Gb带宽,支持IPv6

    2015年4月15日,vps服务商digitalocean宣布正式开通德国法兰克福机房.5个月前digitalocean宣布筹备Germany机房中心,经过近1个月部署,总算与用户见面了.Frankf ...