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 ...
随机推荐
- Java线程池详解(一)
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
- BaaS_后端即服务 RESTful
码云coding API https://open.coding.net/ Swagger 官网用VPN能流畅打开,但它自己的基于web的编辑器不行 用来设计RESTful API LeanCloud ...
- CSAPP阅读笔记-gcc常用参数初探-来自第三章3.2的笔记-P113
gcc是一种C编译器,这次我们根据书上的代码尝试着使用它. 使用之前,先补充前置知识.编译器将源代码转换为可执行代码的流程:首先,预处理器对源代码进行处理,将#define指定的宏进行替换,将#inc ...
- (转)Linux磁盘空间监控告警 && Linux磁盘管理
Linux磁盘空间监控告警 http://blog.csdn.net/github_39069288/article/details/73478784-----------Linux磁盘管理 原文:h ...
- linux命令行下的操作的快捷键
历史相关命令 命令 含义!! 执行上一条命令!num 执行历史命令中的第num条命令!-n ...
- mac上k8s学习踩坑
本文学习k8s参考内容:http://docs.kubernetes.org.cn/126.html,学习过程中遇到一些坑,记录如下: -------------------------------- ...
- 读《NoSQL精粹》前三章有感
现在NoSQL很流行,所以买了一本这方面的书,这本书虽然很薄 156页,但是感觉的确是大师的经验之谈,对于自己经验还是很少.无法能完全能心领神会,大师所说的,就像一个人说药苦,你没吃过.再听别人描述也 ...
- 简单的一个php验证登陆代码
<?php/** */ if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER ...
- phpstorm主题设置
毫无疑问,phpstorm很好用,但是安装完成后自带的主题,丑的一匹,所以总结下如何更换主题............. 1.主题下载位置 http://www.phpstorm-themes.com ...
- small zhishi
\\192.168.1.201\d$\Data 访问远程计算机文件资源管理器