【题目】

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.

【题意】

给定的二叉搜索树中有两个节点的值错换了,找出这两个节点。恢复二叉搜索树。要求不适用额外的空间。

【思路】

中序遍历二叉树。一棵正常的二叉树中序遍历得到有序的序列,现有两个节点的值的调换了,则肯定是一个较大的值被放到了序列的前段。而较小的值被放到了序列的后段。节点的错换使得序列中出现了s[i-1]>s[i]的情况。假设错换的点正好是相邻的两个数,则s[i-1]>s[i]的情况仅仅出现一次。假设不相邻,则会出现两次,第一次出现是前者为错换的较大值节点,第二次出现时后者为错换的较小值节点。

【代码】

/**
* 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) {
stack<TreeNode*> st;
TreeNode* pointer=root;
TreeNode* prev=NULL;
TreeNode* nodeLarge=NULL;
TreeNode* nodeSmall=NULL;
while(pointer){st.push(pointer); pointer=pointer->left;}
while(!st.empty()){
TreeNode* cur = st.top();
st.pop();
if(prev && prev->val > cur->val){
if(nodeLarge==NULL || prev->val > nodeLarge->val) nodeLarge=prev;
if(nodeSmall==NULL || cur->val < nodeSmall->val) nodeSmall=cur;
}
prev=cur;
pointer=cur->right;
while(pointer){st.push(pointer); pointer=pointer->left;}
}
//替换两个节点的值
int temp=nodeLarge->val;
nodeLarge->val = nodeSmall->val;
nodeSmall->val = temp;
}
};

LeetCode: Recover Binary Search Tree [099]的更多相关文章

  1. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  2. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. [leetcode]Recover Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...

  4. [Leetcode] Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误

    Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...

  7. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

随机推荐

  1. 切换myEclipse工作空间后设置,myEclipse添加注释/设置豆沙背景颜色/调节字体大小

    一.添加注释 操作位置: 注释规范 Files/** * @文件名称: ${file_name} * @文件路径: ${package_name} * @功能描述: ${todo} * @作者: ${ ...

  2. moment.js(moment-in-node.js)获取本月最后一天 不指定

    http://tommyhu.cn/moment-in-nodejs/ //获取本月最后一天 to=using.moment(日期).endOf('month').format("YYYY- ...

  3. 牛客网Java刷题知识点之Map的两种取值方式keySet和entrySet、HashMap 、Hashtable、TreeMap、LinkedHashMap、ConcurrentHashMap 、WeakHashMap

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

  4. 阿里云Centos 7上面安装mysql教程

    1 软件的基本安装过程 1 卸载已有的mysql 1.查看系统是否安装了mysql软件 rpm -qa|grep -i mysql 2.将已经安装过的软件卸载掉.注意:这样的卸载是不彻底,不过这里够用 ...

  5. oracle中的exists理解

    select * from EB where exists (select * from BB where Code=EB.Code) 把select 外层表EB看成是循环的,把每一个值eb.code ...

  6. linux_api之进程环境

    本篇索引: 1.引言 2.main函数 3.进程的终止方式 4.exit和_exit函数 5.atexit函数 7.环境表 8.C程序程序空间布局 9.存储空间的手动分配 10.库文件 1.引言 一个 ...

  7. 简单的一个php验证登陆代码

    <?php/** */ if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER ...

  8. hdu 2845——Beans——————【dp】

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. maven 基本配置

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  10. SQL脚本整理系列 三

    触发器 SQL 2008 怎么实现删除学生表里面的一条记录,成绩表里面关于这个学生的记录也同时删掉,谢求具体代码 --创建表 DROP TABLE tstudent GO CREATE TABLE t ...