[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 ...
随机推荐
- vertical-align垂直居中
<div id="content"> <div id="weizi"> 锄禾日当午,<br> 汗滴禾下土.<br> ...
- Java : 实体类不能序列化异常
当修改实体类之后调用接口出现不能序列化的异常时,一定要检查实体之间的关系是否都是正确的. could not serialize; nested exception is org.hibernate. ...
- video.js使用技巧
https://www.awaimai.com/2053.html https://www.jianshu.com/p/16fa00a1ca8e
- mysql日志管理#二进制日志详解
查看MySQL二进制文件中的内容有两种方式 mysqlbinlog SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row ...
- flask(列表实现)
在 index/views.py 中定义视图函数 在查询的时候,如果用户分类id传0,则不添加分类查询条件 @index_blu.route('/newslist') def get_news_lis ...
- sqli-labs 1-20实验记录
1. less1 首先输入?id=1 查找是否有注入点. 输入单引号 回显报错 说明有注入漏洞 而且是数字型 输入 1’ or 1=1 order by 1 猜测列名# 这里发现#不能变成url编码 ...
- java基础day05---界面
java基础day05---界面 1.GUI:图形用户界面(Graphics User Interface) 开发工具包AWT抽象窗口把工具箱===>swing 解决了awt存在的lcd问题== ...
- go学习笔记-函数
函数 定义 格式 func function_name( [parameter list] ) [return_types] { 函数体 } 解析 func:函数由 func 开始声明 functio ...
- CS61B sp2018笔记 | Lists
Lists csdn同作者原创地址 1. IntLists 下面我们来一步一步的实现List类,首先你可以实现一个最简单的版本: public class IntList { public int ...
- mysql用命令创建用户创建数据库设置权限
1.create database bbs; //创建数据库 2.create user bbs IDENTIFIED by 'bbs'; //创建用户bbs和登录密码bbs 3.grant AL ...