【题目】

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. knime 设置 小数点精度

    kinme 默认小数精度是保留三位小数. 如果0.0003,knime会自动舍弃,读出0.下面步骤教你怎么把小数精度全部显示. File->references->preferred re ...

  2. 网站ico那点事儿

    一. 如何获取某个网站的favicon.ico http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看 ...

  3. setContentView和inflate区别

    一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象.其中有一个比较常用,View infl ...

  4. Java中的时间处理

    日期时间组件使用 java.util.Date:实现类,其对象具有时间.日期组件.java.util.Calendar:抽象类,其对象具有时间.日期组件.java.sql.Date:实现类,其对象具有 ...

  5. css样式查找遇到的问题汇总

    利用css简单排除元素的第一个子元素 例如:排除表格的第一行 /*除了表格的第一行其他都显示为红色*/ table tr+tr{ background-color:red;/*除了表格的第一行其他都显 ...

  6. win10x系统下的Git下载安装

    git安装和使用百度一下就有,官方地址https://git-scm.com/book/zh/v1/起步-安装-Git 但是说的并不是很详细,自己记录一下, 首先我们去官网下载一个git 有两个下载地 ...

  7. js之正则表达式基础

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  8. 位运算(6)——Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  9. shell 复制/备份文件 脚本

    #!/bin/sh # author hapday # -- echo "以时间日期为名称基准备份 war 包." date +%Y-%m-%d-%H-%M-%S cp artup ...

  10. 关于easyui 圆角按钮在ie9不能隐藏

    (一)在easyui框架里 在a标签里添加class='easyui-linkbutton' 如: (二)问题:在ie9里圆角背景overflow:hidden 不起作用 (a)框架内 html 部分 ...