https://oj.leetcode.com/problems/recover-binary-search-tree/

一棵二叉搜索树,二叉搜索树的特征是,中根遍历的话,得到的序列是递增的

题目中,有两个节点弄混了,让恢复这个二叉搜索树

class Solution {
public:
TreeNode *LastNode = new TreeNode(INT_MIN); void recoverTree(TreeNode *root) {
if(root == NULL)
return; TreeNode **n1 = (TreeNode**)malloc(sizeof(TreeNode*));
TreeNode **n2 = (TreeNode**)malloc(sizeof(TreeNode*));
*n1 = NULL;
*n2 = NULL;
find(root,n1,n2);
//swap
int temp;
temp = (*n1)->val; (*n1)->val = (*n2)->val; (*n2)->val = temp;
} //中根遍历
void find(TreeNode *root, TreeNode **n1, TreeNode **n2)
{
if(root == NULL)
return; find(root->left,n1,n2);

//分别处理了是 2 1 还是,3 2 1 的情况,高!
if(*n1 == NULL && root->val < LastNode->val)
{
*n1 = LastNode;
}
if(*n1 != NULL && root->val < LastNode->val)
{
*n2 = root;
} LastNode = root; find(root->right,n1,n2);
}
};

  

LeetCode OJ-- Recover Binary Search Tree ***@的更多相关文章

  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】Recover Binary Search Tree

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

  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][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

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

  5. leetcode 99 Recover Binary Search Tree ----- java

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

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

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

  7. Java for LeetCode 099 Recover Binary Search Tree

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

  8. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. LeetCode OJ——Validate Binary Search Tree

    http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...

  10. Leetcode#99 Recover Binary Search Tree

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

随机推荐

  1. 如何使用pyinstaller打包32位的exe

    说明:原来安装的python为64位,故安装的pyinstaller和打包后的exe都为64位.而64位的exe文件在32位的win7操作系统下是无法执行的,显示不兼容.网上查询发现,简单(可能不方便 ...

  2. Python中列表的深浅拷贝

    copy_lst = [ ('py对象三要素',), ('== 比较运算符',), ('is 身份运算符',), ('小数据池',), ('列表的浅拷贝',), ('列表的深拷贝',), ] py对象 ...

  3. Labyrinth POJ - 1383

    Labyrinth POJ - 1383 The northern part of the Pyramid contains a very large and complicated labyrint ...

  4. hihocoder#1098 : 最小生成树二·Kruscal算法

    #1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...

  5. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  6. Wireshark启动出现“无法启动此程序,因为计算机丢失api-ms-win-crt-runtime-l1-1-0.dll。”

    由于重装了win7系统,安装wireshark启动出现了“无法启动此程序,因为计算机丢api-ms-win-crt-runtime-l1-1-0.dll”的问题. 网上查了一圈的资料终解决问题,于是整 ...

  7. appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource

    C:\DevelopSoft\workspace\appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource ...

  8. ECharts的x轴和y轴均使用数值类型

    今天有个需求,就是需要ECharts的x轴和y轴都要使用数值类型,即xAxis.type和yAxis.type均为value,然后我按照我以为的方式修改了下,发现图崩了 发现问题: 然后我打开了ECh ...

  9. [netty4][netty-common]Future与Promise分析

    接口与类结构体系 -- [I]java.util.concurrent.Future<V> ---- [I]io.netty.util.concurrent.Future<V> ...

  10. Python+Selenium练习篇之19-多窗口之间切换

    本文来介绍如何处理driver在多窗口之间切换,想一下这样的场景,在页面A点击一个连接,会触发在新Tab或者新窗口打开页面B,由于之前的driver实例对象在页面A,但是你接下来的脚本是操作页面B的元 ...