Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

互换二叉搜索树中两个位置错误的节点。

思路:

预设三个指针pre,p1,p2。p1用来指向第一个出错的节点,p2用来指向第二个出错的节点。

出错情况有两种,即p1和p2相邻,p1和p2不相邻。

中序遍历此二叉树,用pre指向当前节点的上一个节点,如果出错的节点相邻,此时p1应该指向pre,p2应该指向当前节点root。

若出错节点不相邻,则用p1记录第一个出错节点pre,继续遍历到前一个节点pre大于当前节点root时,用p2指向第二个出错节点p2.

代码如下:

class Solution {
public:
TreeNode *pre,*p1,*p2; void run(TreeNode *root){
if(!root)
return;
run(root->left);
if(pre && pre->val > root->val){
if(p1 == NULL){
p1 = pre; p2 = root;
}
else{
p2 = root;
}
}
pre = root;
run(root->right);
} void recoverTree(TreeNode *root) {
if(!root)
return ;
pre = p1 = p2 = NULL;
run(root);
swap(p1->val,p2->val);
}
};

【LeetCode练习题】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#99 Recover Binary Search Tree

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

  9. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

随机推荐

  1. centos无线网卡设置

    联想thinkpad E430 ,安装上centos后,无线网卡一直无法使用,经历了艰苦曲折到网卡驱动安装过程,历时2个晚上终于开心到用上无线网卡. 1.查看无线网卡到具体型号 [root@local ...

  2. (转)iOS7界面设计规范(8) - UI基础 - 术语和措辞

    讨厌周一,讨厌一周.今天中午交互组聚餐,却很开心:大家都是很厉害的人,你可以感到他们身上的能量,可以感到有些什么东西正在推着自己尽力向前走.这是一种很健康的状态,同时也很难得,自然越发需要珍惜.从无到 ...

  3. windows常用net use命令

    net share :查看本地主机的共资源 nbtstat -A IP :得到远程主机的用户列表 net user c:/del 删除映射的C盘,其它盘类推 net user * /del 删除全部映 ...

  4. DEV PivotGridControl 全选行或列

    foreach (string item in fieldProductName.FilterValues.Values) { pivotGridControl.Cells.SetSelectionB ...

  5. DataTable复制自身行

    在我们工作的过程中有可能要使用DataTable产生一些重复数据(在不重复读取数据库的情况下) 无废话,直接上代码 DataTable复制自身一行(目的产生重复数据),已测试通过可直接复制 /// & ...

  6. STL set容器添加结构体并排序

    #include <iostream> #include <string> #include <cstring> //strcpy #include <cst ...

  7. bootstrap-paginator 分页控件的使用

    首先对js和css的引用 <link rel="stylesheet" href="reference/bootstrap/css/bootstrap.min.cs ...

  8. shell中的if语句

    语法格式 if command;then commands fi 其中的command包含如下: shell command 任何shell命令,如果shell命令返回0,代表true,否则,代表fa ...

  9. SQL学习笔记——SQL中的数据查询语句汇总

    where条件表达式 --统计函数 Select count(1) from student; --like模糊查询 --统计班上姓张的人数 select count(*) from student ...

  10. No2_4.接口继承多态_Java学习笔记_经典案例

    import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import jav ...