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. Wex5页面事件执行顺序

    wex5 事件执行顺序data组件的onCustomRefresh→ model组件的onLoad→ windowReceiver组件的onReceive

  2. 自定义view中错误:No resource identifier found for attribute X in package X

  3. GIS简单计算Helper类

    using System; using ESRI.ArcGIS.Client.Geometry; namespace GISProject.Extensions { /// <summary&g ...

  4. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  5. 夺命雷公狗-----React---13--事件监听

    在react中事件监听直接作为组建的属性来添加即可,就像DOM中的html操作 <!DOCTYPE> <html> <head> <meta charset= ...

  6. Python爬虫--简单爬取图片

    今天晚上弄了一个简单的爬虫,可以爬取网页的图片,现在现在做一下准备工作. 需要的库:urllib 和 re urllib库可以理解为是一个url下载器,其中有三个重要的方法 urllib.urlope ...

  7. [Phalcon] Phalcon系统默认事件列表

    版本: 2.0.6 Phalcon\Mvc\Application application:boot 可终止 是 参数 Phalcon\Events\Event $event 事件本身 Phalcon ...

  8. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数007, match,图像匹配

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数007, match,图像匹配 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换 ...

  9. ajax+jquery+ashx如何实现上传文件

    第一:建立Default.aspx页面 <html> <head runat="server"> <title>ajax图片上传</tit ...

  10. SQL2005中的事务与锁定(七) - 转载

    ------------------------------------------------------------------------ -- Author : HappyFlyStone - ...