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?

中序遍历解法O(n)space
 
 /**
* 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 *> nodes;
dfs(root,nodes);
int index1=-;
int index2=-;
for(int i=;i<nodes.size();i++)
{
if(nodes[i]->val<nodes[i-]->val)
{
if(index1==-) index1=i-;
index2=i;
}
} int tmp=nodes[index1]->val;
nodes[index1]->val=nodes[index2]->val;
nodes[index2]->val=tmp;
return; } void dfs(TreeNode *root,vector<TreeNode *> &nodes)
{
if(root==NULL) return;
dfs(root->left,nodes);
nodes.push_back(root);
dfs(root->right,nodes);
}
};
 
 
直接在中序遍历中进行判断,需要记录遍历中的前一个元素
如果前一个元素比当前元素大,则说明存在错误

 /**
* 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) { TreeNode *firstError=NULL;
TreeNode *secondError=NULL;
TreeNode *pre=NULL; dfs(root,pre,firstError,secondError); int tmp=firstError->val;
firstError->val=secondError->val;
secondError->val=tmp;
} void dfs(TreeNode *root,TreeNode *&pre,TreeNode *&firstError,TreeNode *&secondError)
{
if(root==NULL) return; dfs(root->left,pre,firstError,secondError); if(pre!=NULL&&pre->val>root->val)
{
if(firstError==NULL) firstError=pre;
secondError=root;
}
pre=root; dfs(root->right,pre,firstError,secondError);
}
};

【leetcode】Recover Binary Search Tree的更多相关文章

  1. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  2. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  3. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  4. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. 【题解】【BST】【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 ...

  7. 【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 ...

  8. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  9. 【LeetCode】Validate Binary Search Tree 二叉查找树的推断

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...

随机推荐

  1. servlet request getHeader(“x-forwarded-for”) 获取真实IP

    request方法客户端IP: request.getRemoteAddr() 输出:192.168.0.106 客户端主机名:request.getRemoteHost()输出:abc reques ...

  2. TableView的执行流程 & 位移枚举

    // 闲来无聊测试一下 第一轮: 1.numberOfSectionsInTableView    :假如section=2,此函数只执行一次,假如section=0,下面函数不执行,默认为1 2.h ...

  3. 将Ubuntu 15.10升级到Ubuntu 16.04

    Ubuntu 16.04 LTS 代号为 Xenial Xerus,其最终版将于 2016 年 4 月 21 日正式发布,Ubuntu16.04 将是非常受欢迎的开源操作系统 Ubuntu 的第 6 ...

  4. motto1

    不要失去了才懂得珍惜.很多东西是不能再来的,又有很多东西再来是要付出代价的,所以,好好珍惜你现在拥有的一切,努力去争取你现在没有的.

  5. MySql循环插入数据(定义了存储过程)

    MySQL一窍不通啊,今天工作上需要用到,请教了别人,做以备忘 DROP PROCEDURE test_insert ; DELIMITER ;; CREATE PROCEDURE test_inse ...

  6. SQL补充

    TOP 子句TOP 子句用于规定要返回的记录的数目.对于拥有数千条记录的大型表来说,TOP 子句是非常有用的.注释:并非所有的数据库系统都支持 TOP 子句.SELECT TOP 2 * FROM P ...

  7. ie下获取上传文件全路径

    ie下获取上传文件全路径,3.5之后的火狐是没法获取上传文件全路径的 /*获取上传文件路径*/ function getFilePath(obj) { var form = $(this).paren ...

  8. Mac Pro 解压安装MySQL二进制分发版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)

    没有mac的root密码,当前用户有sudo权限,所以想以root身份执行的命令都加了sudo. 是否存在 _mysql 用户和用户组,并查看用户 _mysql 是不是用户组 _mysql 的成员. ...

  9. PPPoE名词解释

    PPPoE拔号的发现阶段(Discovery): PPPoE的发现阶段一共分为4步. 分别是: PADI(PPPoE Active Discovery Initiation) PADO(PPPoE A ...

  10. Js控制iFrame切换加载网址

    <html> <head> <title>Js控制 iFrame 切换加载网址</title> </head> <body> & ...