[Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake.
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.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void recoverTree(TreeNode *root)
{
vector<TreeNode *> treeNode;
vector<int> nodeVal;
inoder(root,treeNode,nodeVal);
sort(nodeVal.begin(),nodeVal.end());
for(int i=;i<treeNode.size();++i)
treeNode[i]->val=nodeVal[i];
} void inoder(TreeNode *root,vector<TreeNode *> &treeNode,vector<int> &nodeVal)
{
if(root==NULL) return;
inoder(root->left,treeNode,nodeVal);
treeNode.push_back(root);
nodeVal.push_back(root->val);
inoder(root->right,treeNode,nodeVal);
}
};
方法二:
用三个指针,w1,w2分别指向第一、二个错误的地方。pre指向当前结点中序遍历中的前一个结点。因有两个结点交换了,所以二叉树的中序遍历中会出现违背有序的情况,一、即中序遍历中相邻的结点被交换,则违背有序的情况出现一次,如132456;二、中序遍历中不相邻的两个结点的值被交换,则出现两次违背有序情况,如153426.针对情况1,pre=3,w1=pre即为3,w2=root,即为2,在后续的遍历中没有违背有序的情况,所以交换w1和w2即可;针对情况2,找到第一个错误点后,w1 !=NULL了,所以,第二个错误点w2=root,第一逆序的前结点,第二逆序的后结点,交换两者即可。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *w1,*w2,*pre;
void recoverTree(TreeNode *root)
{
if(root==NULL) return;
w1=w2=pre=NULL;
find(root);
swap(w1->val,w2->val);
}
void find(TreeNode *root)
{
if(root==NULL) return;
find(root->left);
if(pre&&pre->val > root->val)
{
if(w1==NULL) //情况1
{
w1=pre;
w2=root;
}
else //情况2
w2=root;
}
pre=root;
find(root->right);
}
};
方法三
层次遍历中的Mirror方法,修改部分得到。真正的O(1)。
// Now O(1) space complexity
class Solution {
public:
void recoverTree(TreeNode *root) {
TreeNode *first = NULL, *second = NULL, *parent = NULL;
TreeNode *cur, *pre;
cur = root;
while (cur) {
if (!cur->left) {
if (parent && parent->val > cur->val) {
if (!first) first = parent;
second = cur;
}
parent = cur;
cur = cur->right;
} else {
pre = cur->left;
while (pre->right && pre->right != cur) pre = pre->right;
if (!pre->right) {
pre->right = cur;
cur = cur->left;
} else {
pre->right = NULL;
if (parent->val > cur->val) {
if (!first) first = parent;
second = cur;
}
parent = cur;
cur = cur->right;
}
}
}
if (first && second) swap(first->val, second->val);
}
};
[Leetcode] Recover binary search tree 恢复二叉搜索树的更多相关文章
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 099 Recover Binary Search Tree 复原二叉搜索树
二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树. 详见:https://leetcode.com/problems/recover-binary-search-tree/submission ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- laravel 增删改查 数据库设置 路由设置
laravel 框架的路由设置: url: http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs laravel ...
- Scrapy核心组件
• 引擎(Scrapy)用来处理整个系统的数据流处理, 触发事务(框架核心) • 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个UR ...
- 005---Python数据类型--字典
字典 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px ...
- Fragment保持状态切换
在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常 ...
- Java-Swing中使用Web富文本编辑器
资料下载 (截取出了邮件发送的功能.) 2018/11/10 因为要 win7 电脑 IE 8 的原因,使用了 jxBrower 拓展,更容易使用,参考链接(推荐) 问题介绍 window客户端软件的 ...
- 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(三):设置上传文件夹权限(这里测试用完全共享)
基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django 基于Ubuntu Server 16.04 LTS版本安装和部署Djan ...
- 【转】mui 通过JSON动态的生成列表
<script type="text/template" id="radio-tigan"> <%for(var i=0;i<recor ...
- Borland和Micorsoft的对话(转载自月光软件网)
Borland与Microsoft关于Delphi的对话 Bear 1.Delphi较贵 一套Delphi的价格大约相当于两套Visual Studio ------------------- ...
- Linux上Makefile管理java项目
前面文章讲到了Linux上通过.spec文件与rpmbuild命令将java程序打包为RPM安装包, 现阶段遇到新的需求: 使用Makefile来操纵java的编译.打包 该需求以前面的内容为基础 可 ...
- 把python脚本打包成win可执行文件
前几天有个朋友找我写一点小东西,写好后把代码发他帮他搞了半天,结果愣是没听懂,就找到了这个办法. 1.导入pyinstaller包, pip install pyinstaller 2.进入到你需要打 ...