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. MongDB之各种删除操作

    接口IMongDaoDelete: package com.net.test.mongdb.dao; public interface IMongDaoDelete { public void del ...

  2. 内存管理小结(2)--伙伴系统API

    伙伴系统分配内存以2的整数幂次的页数为单位.提供的API主要分为分配类与释放类. 1.分配类 1.1unsigned long __get_free_pages(gfp_t gfp_mask, uns ...

  3. Codeforces Round #462 (Div. 2) C. A Twisty Movement

    C. A Twisty Movement time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  4. 动态规划:HDU1003-Max Sum(最大子序列和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. CF 497 div 2 B

    B. Turn the Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. Android Kotlin适用小函数

    都是一些Android适用的Kotlin小函数. 1.点击空白隐藏键盘 //点击空白隐藏键盘 override fun onTouchEvent(event: MotionEvent): Boolea ...

  7. MySQL之索引(一)

    创建高性能索引 索引是存储引擎用于快速找到记录的一种数据结构.这是索引的基本功能.索引对于良好的性能非常关键.尤其是当表中的数据量越来越大时,索引对性能的影响愈发重要.在数据量较小且负载较低时,不恰当 ...

  8. ajax跨域请求的处理

    跨域的情形有很多种,网上有人给出了一份表格, 表格中标识为”不允许”通信的情况都属于跨域.实际网络服务中需要跨域的情况确实存在,于是开发者们提供了一种解决方案,就是使用jsonp格式进行数据交互,它不 ...

  9. Django基础之数据库与ORM

    一.数据库配置 1.django默认支持sqlite,mysql, oracle,postgresql数据库. django默认使用sqlite的数据库,默认自带sqlite的数据库驱动 , 引擎名称 ...

  10. SPFA - Luogu 3385 【模板】负环

    [模板]负环 描述 找负环 输入 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每行三个整数a b w,表示a->b有一条权值为w ...