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. kubernetes dashboard permission errors

    kubernetes dashboard 的权限错误 warning configmaps is forbidden: User "system:serviceaccount:kube-sy ...

  2. django_mysql_配置

    配置 1. 安装Pymysql pip install PyMySQL 然后在项目同名_init__添加 from pymysql import install_as_MySQLdb install_ ...

  3. django开发基础

    一.配置静态文件 https://www.cnblogs.com/lshedward/p/10351051.html 二.路由分发 https://www.cnblogs.com/lshedward/ ...

  4. B1007 素数对猜想

    B1007 素数对猜想 让我们定义\(d_n\)为:\(d_n =p_{n+1}−p_n\),其中\(p_i\)是第i个素数.显然有\(d_1=1\),且对于n>1有\(d_n\)是偶数.&qu ...

  5. 动态规划:HDU1160-FatMouse's Speed(记录动态规划状态转移过程)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 源码级强力分析hadoop的RPC机制

    分析对象: hadoop版本:hadoop 0.20.203.0 必备技术点: 1. 动态代理(参考 :http://weixiaolu.iteye.com/blog/1477774 )2. Java ...

  7. billard:桌球的走位路线图解

    这些是桌球的一些基础知识,记得刚学会桌球那会儿很强烈的想找到类似图片或资料,好久都找不到,最严重的时候只要一闭上眼睛,满脑子就是桌球的路线,线路图几乎是无处不在,痛苦的是经常能理解过来的很多路线因为杆 ...

  8. Mysql密码加密方式

    以Mysql 4.1版本为分界线,两种加密方式 Mysql323加密:(16位) select  old_password('root'); //Mysql自带加密函数old_password(str ...

  9. dotnet core 2.2 安装后在vs2017中无法选择 dotnet core 2.2 为目标框架

    可能有效的解决方案: 无法完全保证有效,因为我本地装上没问题,只帮同事解决过一次,貌似有效 方案就是多装几个 .net core 2.2.xxx 版本,然后可能就正常识别了. 在安装之前,先把 vs ...

  10. 分治 - 计算几何 - BZOJ2458,[BeiJing2011]最小三角形

    http://www.lydsy.com/JudgeOnline/problem.php?id=2458 [BeiJing2011]最小三角形 描述 Frisk现在遇到了一个有趣的问题. 平面上有N个 ...