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. 数据结构-队列(Queue)

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...

  2. Python文件与异常处理

    文件读写 使用python的BIF(build in function)open()进行文件读写操作 # 1.打开文件 data = open(file_name,'w') # 读取模式有很多种,主要 ...

  3. [原]sencha touch之NavigationView

    还是直接上代码,都是基本的几个容器控件,没什么还说的 Ext.application({ name:'itkingApp', launch:function(){ var view =Ext.crea ...

  4. Spring---浅谈AOP

    概念 AOP是Aspect Oriented Programming的缩写,即面向切面的编程.是一种比较新颖的编程思想,也是Spring框架中一个重要的领域. AOP将应用系统分为两个部分:核心业务逻 ...

  5. P3376 【模板】网络最大流dinic算法

    P3376 [模板]网络最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点 ...

  6. Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)

    什么是NIC Teaming?         NIC Teaming 就是将两个或更多的网络适配器组合在一起,从而达到容错和带宽聚合作用.NIC Teaming 中的每个网络适配器都是物理存在的(虚 ...

  7. C#入门篇-3:数据类型及转换

    using System; using System.Text; using System.Collections; using System.Collections.Generic; //003 查 ...

  8. IOS开发学习笔记015-block和protocol

    一.block block 代码段    标识是 ^    block 和函数很像 1.可以保存代码 2.有返回值 3.有形参 格式 返回值 (block名)(形参列表) = ^(形参列表) {代码段 ...

  9. Java 第七次

  10. Spring整合hibernate -声明事务管理

     目录 1 sessionFactory 注入HibernateTransactionManager 2 XML配置的配置 3 添加annotation-driven 4 引入JAR包 5在servi ...