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. JavaWeb学习总结(一)——JavaWeb开发入门

    http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...

  2. Orchard源码分析(3):Orchard.WarmupStarter程序集

    概述 Orchard.WarmupStarter程序集包含三个类:WarmupUtility.WarmupHttpModule和Starter<T>.该程序集主要为Orchard应用启动初 ...

  3. Linq 中的 left join

    Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...

  4. Spring利用JDBCTemplate实现批量插入和返回id

    1.先介绍一下java.sql.Connection接口提供的三个在执行插入语句后可取的自动生成的主键的方法: //第一个是 PreparedStatement prepareStatement(St ...

  5. Gradle笔记——Gradle的简介与安装

    本博客对Gradle进行一个简单的介绍,以及它的安装. Gradle介绍 Gradle是一个基于JVM的构建工具,它提供了: 像Ant一样,通用灵活的构建工具 可以切换的,基于约定的构建框架 强大的多 ...

  6. 详解FindBugs的各项检测器 .

    FindBugs是一个静态分析工具,在程序不需运行的情况下,分析class文件,将字节码与一组缺陷模式进行对比,试图寻找真正的缺陷或者潜在的性能问题.本文档主要详细说明FindBugs 2.0.3版本 ...

  7. linux下设置进程优先级方法!

    Linux系统下提升进程优先级的办法   Linux系统进程的优先级取值:-20 到 19,数越大优先级越低.  可以通过top命令来查看,NI那一列.   改变进程的优先级的方法有两种:   www ...

  8. mongodb 与 c++ 的配合使用

    最近在尝试使用 mongodb 作为服务端持久化方案,服务端程序是使用 c++ 写的,折腾了不少时间,记录一下吧. 1.下载 boost 1.56.0 http://www.boost.org/use ...

  9. 什么是SEM?

    SEM是Search Engine Marketing的英文缩写,其中文意思就是搜索引擎营销.台湾和香港.澳门也称为搜寻销售,意思都差不多.SEM更多强调的是综合手段在搜索引擎上的企业传播和促进和销售 ...

  10. Python中MySQLdb的事务处理

    今天写了个tool,目的是把csv中的数据插入到数据库中去.其中有一部分,是需要分别向两张表中插入两条数据,如果第二张表中的数据已经存在,那么第一张表中的数据也不需要插入. 然后通过百度查找发现,其实 ...