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?

给定一个排序二叉树,然后任意交换了其中两个节点,要求在使用常数个空间和不改变结构的前提下恢复原来的二叉树。

如果用O(n)的空间复杂度,那么直接中序遍历就好了,现在用常数个,那么只需要记录两次就好了,中序遍历的时候,需要动动脑子。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode pre;
TreeNode first;
TreeNode second; public void inorder(TreeNode root){
if(root == null){
return;
}
inorder(root.left);
if(pre == null){
pre = root;
}else{
if(pre.val > root.val){
if(first == null){
first = pre;
}
second = root;
}
pre = root;
}
inorder(root.right);
} public void recoverTree(TreeNode root) {
pre = null;
first = null;
second = null;
inorder(root);
if(first!=null && second!=null){
int tmp = first.val;
first.val = second.val;
second.val = tmp;
}
} }

leetcode 99 Recover Binary Search Tree ----- java的更多相关文章

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

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

  2. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

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

  3. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

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

  4. Leetcode#99 Recover Binary Search Tree

    原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...

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

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

  6. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. 【LeetCode】99. Recover Binary Search Tree

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

  9. 【leetcode】Recover Binary Search Tree

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

随机推荐

  1. X230 安装win7 sp1

    早上起床发现win10歇菜了,死活启动不了只好重装系统 用稳定版本win7比较靠谱. 去msdn上下载 一个win7系统 win7旗舰版本64位 ed2k://|file|cn_windows_7_u ...

  2. Oracle创建用户并给用户授权查询指定表或视图的权限

    MSV31账户登录数据库进行如下操作: CREATE USER NORTHBOUND IDENTIFIED BY NORTHBOUND  DEFAULT TABLESPACE "TBS_DN ...

  3. [pjsip]Pjlib中的链表结构

    Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: 图1:一般链表结构 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表 ...

  4. C# Delete Url Cookie

    public static void DeleteCookieFile(Uri url) { string path = Environment.GetFolderPath(Environment.S ...

  5. MVC5 烂笔头

    HttpContent Controller:HttpContextBase View:HttpContext.Current View的搜寻顺序:本文件夹.本共享.根共享等 class=" ...

  6. javaweb-dbcp2

    package cn.itcast.utils; import java.io.InputStream;import java.sql.Connection;import java.sql.Drive ...

  7. __set()与__get() 魔术方法

    在面向对象编程的过程中,对于类当中的各个成员变量,都有不同的访问属性,比如公有的(public)属性,在类内部和类外部都可直接调用:而私有的(private)和受保护的(protected),在类外不 ...

  8. PHP_Const

    PHP_Const 常量 规则: 1 总是大写 2 A-Z 及 从127-255的ASCII字符 3 全局范围 4 用define函数定义 5 只能包含标量数据 如Boolean integer fl ...

  9. fibonacci数列(二)_矩阵快速幂

    描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...

  10. Java 集合深入理解(4):List<E> 接口

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 蓝瘦!香菇! 连着加班几天,醉了.学学 List 放松下! 在 Java 集合深入理解:Collection 中我们熟悉了 ...