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 ...
随机推荐
- 深入理解CommonJS!
CommonJS 一开始大家都认为JS是辣鸡,没什么用,官方定义的API只能构建基于浏览器的应用程序,CommonJS就按耐不住了,CommonJS API定义很多普通应用程序(主要指非浏览器的应用) ...
- 一起來玩鳥 Starling Framework(3)Button!
週末夜來介紹個簡單的DisplayObject就好.不論是在電腦上或行動裝置上,跟使用者互動次數最多的,大概就是按鈕了.因此,Starling有個Button類別,來做出這個常用的互動元件. 同樣是因 ...
- [Python爬虫] 之十七:Selenium +phantomjs 利用 pyquery抓取梅花网数据
一.介绍 本例子用Selenium +phantomjs爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...
- float:center???
老规矩,先上图(请忽略图中文字^V^): 乍一看感觉是对中间的图片使用了float:center;可是细致一想float属性是没有center这个值的. 那是怎么实现的呢?我一步一步拆给大家看. 1. ...
- Yii2 使用十二 配合ajaxFileUpload 上传文件
1.js $("input#upload").change(function () { $.ajaxFileUpload({ url: '/members/web-members- ...
- knowledgeroot
knowledgeroot 示例站点 www.globaladmin.cn Knowledgeroot可用于文档管理,知识库管理. 1.基于php开发,支持linux ,windows.2.支持mys ...
- Java8 对多个异步任务进行流水线操作(笔记)
现在我们要对商店商品进行折扣服务.每个折扣代码对应不同的折扣率,使用一个枚举变量Discount.Code来实现这一想法,具体代码如下所示. 以枚举类型定义的折扣代码 /** * 折扣服务api * ...
- UNIX管道符
在Unxi操作系统中,标准输入和标准输出是外壳程序中可以单独使用的两个独立流.但是有时候系统工程师需要让某些特定的输入源不起作用.当系统工程师在开发一些实用的脚本程序的过程中,经常需要用到. 一. ...
- unity, use particleSystem with UGUI
要想在UGUI上添加particleSystem,需要将Canvas的Render Mode设置为Screen Space - Camera,并为其Render Camera指定一个Orthograp ...
- Atitit.android webview h5运行环境总结
Atitit.android webview h5运行环境总结 1. WebView 的使用1 2. Js调用java1 3. Js调用java 跟个swt的比较2 3.1. Swt是BrowserF ...