LeetCode: Recover Binary Search Tree [099]
【题目】
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]的更多相关文章
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- [Leetcode] 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 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,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
随机推荐
- PIE SDK Command、Tool、Control的调用和拓展
1.功能简介 在一个项目中,是通过小组成员共同开发的,难以避免的是当项目功能集成的时候会出现很多兼容性问题,开发讲究高内聚低耦合,利用Command.Tool和Control的使用,可以提升集成的效率 ...
- java.lang.IllegalArgumentException: Result Maps collection already contains value for xxx
本人项目产生此问题的原因是: 本地备份了一份xxxmapper.xml的副本“xxxmapper - 副本.xml”,应该是系统会自动加载“mappe”目录下的所有xml文件. 参考:https:// ...
- apache ftpserver外网访问配置
apache ftpserver搭建ftp服务非常简单,若只是内网访问,几乎不需要配置,直接启动即可.但若需要外网访问,则需要注意以下几点. 1.若是外网访问,主动模式是不行的,因为客户端报告给服务器 ...
- 使用Verilog描述RTL图
题目要求 分别用两种方式表达此电路: 1)在一个模块中用两个过程来表达: 2)用顶层文件和例化语句的形式来表达. 给出下面RTL图的verilog描述. 1)纯过程语句描述 2)纯连续赋值语句描述 参 ...
- lua-遍历集合-ipairs和pairs的区别
--ipairs和pairs的区别arr = {1,3,[5]=5,name="kaikai",age=12, 89}--arr[4]= 23--ipairs--ipairs仅仅遍 ...
- 《Python编程从入门到实践》_第九章_类
创建一个简单的类 根据Dog类创建的每个实列都将存储名字和年龄.我们赋予了每条小狗蹲下(sit())和打滚(roll_over())的能力: class Dog(): ""&quo ...
- 深入理解JavaScript系列(33):设计模式之策略模式
介绍 策略模式定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户. 正文 在理解策略模式之前,我们先来一个例子,一般情况下,如果我们要做数据合法性验证,很 ...
- tomcat和应用集成
将tomcat作为应用的一部分集成到应用中,使得应用可以直接开启http服务,对外提供接口.此时应用程序不必再遵守j2ee中的文件目录格式要求. 此种方式改变了以往先部署tomcat容器,再按照j2e ...
- 使用Spring的AOP实现切面日志
AOP切面日志的使用方式 @Aspect @Component public class HttpAspect { private static final Logger logger = Logge ...
- Junit入门教程
做开发的时候,完成一个接口.方法.函数或者功能等,需要测试,是否有bug,有逻辑错误.这里有两种方案测试 1. 在main中写测试方法 2. 使用开源框架,这里使用的junit main写测试方法优点 ...