LeetCode具体分析 :: Recover Binary Search Tree [Tree]
Recover the tree without changing its structure.
Note:
A solution using O(n)
space is pretty straight forward. Could you devise a constant space solution?
confused what "{1,#,2,3}" means?
>
read more on how binary tree is serialized on OJ.
这里有几个知识点能够注意一下。是个不错的考察BST的题目。
一、要怎样找出这两个有乱序的位置呢?假设直接在树上考察,似乎关系有些模糊;那么,考虑一个性质。通过中序遍历,能够将BST按升序输出,eg:12
3 4 56 7,这里随意调换一下位置。构成16 3 4 5 2 7, 6 和
2将数列切割成了三份。每份独立时仍然保持升序(有能够能头尾的部门不包括元素),可是6 > 3。 5 > 2;说明,第一次出现 pre > cur的情况。pre是那个错误节点。第二次出现 pre > cur的情况。cur是错误节点,记录下来就能够了。
二、有可能调换位置的元素在排序中相邻(树结构图中不一定相邻),一中提到的pre >cur的情况就发生重合,仅仅有一次;
代码例如以下:
class Solution {
private:
TreeNode *Pre, *node1, *node2; //申明为成员变量,这样每一个函数都能够訪问,不必反复创建;
void inOrder(TreeNode *root){
if (root == NULL)
return;
inOrder(root->left);
if (Pre != NULL && Pre->val > root->val){ //第二次(也是最后一次)出现的才是正确位置。这里这样写避免了推断,直接覆盖
node2 = root;
if (node1 == NULL) //利用 NULL 为是否为第一次出现的标记
node1 = Pre;
}
Pre = root;
inOrder(root->right);
}
public:
void recoverTree(TreeNode *root) {
Pre = NULL;
node2 = node1 = NULL;
inOrder(root);
node1->val ^= node2->val;
node2->val ^= node1->val;
node1->val ^= node2->val;
}
};
LeetCode具体分析 :: Recover Binary Search Tree [Tree]的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【一天一道LeetCode】#99. Recover Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode 99】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode OJ 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- http://blog.csdn.net/hahalzb/article/details/5889545
http://blog.csdn.net/hahalzb/article/details/5889545
- scrapy-splash抓取动态数据例子九
一.介绍 本例子用scrapy-splash抓取众视网网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- hdu1863 畅通project(判定最小生成树)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- WP8学习笔记:如何在页面显示前自动转向到其他页面
在本次修练开始之前,我们除了预设的 MainPage页面外,也另外新增了一个 Login页面,如下图示: MainPage.xaml页面长这样 Login.xaml页面长这样 因为我们的需求是要求使用 ...
- 04-1下载Win系统(装机助理)
下载Win系统(装机助理): http://www.zhuangjizhuli.com/upan.html http://www.krlxx.com/64win7.html 选择你需要安装的系统: 以 ...
- 【web开发学习笔记】Structs2 Result学习笔记(一)简介
Structs2 Result学习笔记(一)简介 问题一 <struts> <constant name="struts.devMode" value=" ...
- docker run
1.登录Docker Hub 账户 2.输入命令:构建一个web应用. docker run -d -P training/webapp python app.py 参数说明: -d:让容器在后台运行 ...
- Atitit.atiDataStoreService v2 新特性
Atitit.atiDataStoreService v2 新特性 1.1. V1 基础实现1 1.2. V2 增加了对 $uuid $cur_uid参数的支持1 1.3. 增加了fld ...
- SRIO调试(C6678->SRIO和Virtex6->FPGA)
C6678->SRIO和Virtex6->FPGA 设计的板子到了SRIO调试阶段了,在板子上,一片V6和两片6678通过4XSRIO互联,中间没有Switch,总算搞定了相互之间的通 ...
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...