99. 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?
/**
* Definition for a binary tree node.
* 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*> s;
TreeNode *p = root, *last = NULL, *p1 = NULL, *p2 = NULL;
while(p || !s.empty())
{
while(p)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
if(last && last->val >= p->val)
{
p2 = p;
if(!p1)
p1 = last;
else
break;
}
last = p;
p = p->right;
}
}
swap(p1->val, p2->val);
}
};
99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 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 ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 99. Recover Binary Search Tree
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- LeetCode OJ 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- 【elasticsearch 依赖 urllib3 请问 是否 urllib3和阿里es、oss的对接出现异常】
During handling of the above exception, another exception occurred: Traceback (most recent call last ...
- Python爬虫基础(一)urllib2库的基本使用
爬虫也就是所谓的网络数据采集,是一种通过多种手段收集网络数据的方式,不光是通过与 API 交互(或者直接与浏览器交互)的方式.最常用的方法是写一个自动化程序向网络服务器请求数据(通常是用 HTML 表 ...
- Git 基本操作(二)
1. 分支操作 1.1 Fast-forward 当被合并分支(C4)位于合并分支(C2)的历史线上,此时的合并称为"fast-forward"; // hotfix 被合并到 m ...
- Notepad++插件安装和使用和打开大文件
版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主同意不得转载. https://blog.csdn.net/pipisorry/article/d ...
- git-【一】概述安装
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...
- sql 用xml方式插入数据乱码问题解决方法
sql 使用存储过程 参数为xml字符串 xml不要写编码,如下 <?xml version=\"1.0\" ?><root>数据字符串</root& ...
- jsp内置对象与servlet的关系
Servlet与JSP九大内置对象的关系 JSP对象 怎样获得 out->response.getWriter request ->Service方法中的req参数 response ...
- 3.12 Templates -- Wrting Helpers(编写辅助器)
一.概述 1. Helpers允许你向你的模板添加超出在Ember中开箱即用的额外的功能.辅助器是最有用的,用于将来自模型和组件的原始值转换成更适合于用户的格式. 2. 例如,假设我们有一个Invoi ...
- web服务器配置实践
1.为linux系统分配IP地址:192.168.X.1/24,客户端XP系统IP地址为:192.168.X.2/24,其主要DNS指定为:192.168.X.1. 2.查询本机是否安装了httpd服 ...
- Polya
using namespace std; typedef long long LL; const int MAXN = 1e3 +10; const LL MOD = (LL)1 << 6 ...