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?

Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly just like iterate the ascending sequence. In this way, we can easily find two elements swapped by mistake, then swaps them back.

    void inorder(TreeNode * root, TreeNode ** error_node, TreeNode ** pre_node, int * is_swap){
if( root == NULL )
return;
if(root->left != NULL)
inorder(root -> left, error_node, pre_node, is_swap); if((*pre_node) != NULL) {
if ((*pre_node)->val > root->val){
if ((*error_node) == NULL)
*error_node = *pre_node;
}else if( (*error_node) != NULL && (*error_node)->val < root->val && (*error_node)->val > (*pre_node )->val){
//swap error node and pre node
int tmp = (*error_node)->val;
(*error_node )->val = (*pre_node)->val;
(*pre_node)->val = tmp;
*is_swap = ;
return;
}
} (*pre_node )= root; if( root -> right != NULL)
inorder(root -> right, error_node, pre_node, is_swap);
} void recoverTree(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
TreeNode * error_node = NULL;
TreeNode * pre_node = NULL;
int is_swap = ;
inorder(root, &error_node, &pre_node, &is_swap);
if ( is_swap == && error_node != NULL){
int tmp = error_node -> val;
error_node -> val = pre_node -> val;
pre_node -> val = tmp;
}
}

Recover Binary Search Tree [LeetCode]的更多相关文章

  1. Recover Binary Search Tree leetcode java

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

  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(复原BST) ☆☆☆☆☆

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

  4. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

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

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

  6. 【leetcode】Recover Binary Search Tree

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

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

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

  8. LeetCode: Recover Binary Search Tree 解题报告

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

  9. 【LeetCode】99. Recover Binary Search Tree

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

随机推荐

  1. Android--ViewPager制作APP引导页

    ViewPager使用FragmentStatePagerAdapter做Adapter,引导页使用多Fragment形式. FragmentStatePagerAdapter代码如下: public ...

  2. Nhiberate (一)

    严重参考感谢:@wolfy 操作数据库一直都是直接写SQL语句, 接触的ORM框架也不多,新项目要用数据库,数据库访问采用NHibernate. 1. NHibernate 是基于.Net 的针对关系 ...

  3. C#窗体 流

    流:(I/O)输入输出流 分类:文件流,内存流,网络流 流的操作一般要放在try catch里面,操作文件网络容易出现异常 命名空间:using system .IO;using system .Te ...

  4. MyBatis Generator自动生成的配置及使用

    注意:文件名不能有中文字符,不然不能自动生成 找到MyBatis Generator.rar\MyBatis Generator\eclipse里的features和plugins文件,把这两个文件复 ...

  5. 。。。再战JQuery。。。

    今天从学习JQurery的第一个函数开始!!! JQuery里面的show这个函数很不错,我很喜欢,他的使用方法如下:JQuery对象.show(speed,callback); speed你可以指定 ...

  6. struts1

    1.简单应用示例 导入struts1的jar包,然后配置xml,写java和jsp /struts/WebRoot/Login.jsp <%@ page language="java& ...

  7. 【转】PowerShell入门(七):管道——在命令行上编程

    转至:http://www.cnblogs.com/ceachy/archive/2013/02/22/PowerShell_Pipeline.html 管道对于Shell来说是个化腐朽为神奇的东西, ...

  8. ubuntu 如何 su 到 root(作为 root 用户操作)

    ubuntu 安装后,root用户默认被锁定,不允许登录,也不允许"su"到 root.对于桌面用户来说,这样安全性更高一些,但对于服务器可以设置成"允许 su 到roo ...

  9. tomcat普通用户运行

    网站绑定域名后直接通过域名访问使用的是80端口,因此tomcat须监听80端口,而为了安全起见tomcat一般不用root身份运行,综上,需要以普通用户来运行监听80端口的tomcat.此时就会启动失 ...

  10. WPF:设置MenuItem多种不同状态图标

    需求描述: 给MenuItem内部的子Image设置默认图标(鼠标leave).鼠标hover图标.和选中时的图标. 注:是给Menu内个别MenuItem修改,并且是弹出子菜单. 问题描述: 1)前 ...