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 ...
随机推荐
- spring boot实战(第一篇)第一个案例
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...
- redis cluster 集群畅谈(二)
上一篇http://www.cnblogs.com/qinyujie/p/9029482.html, 主要讲解 redis cluster 集群 搭建,本篇主要讲解实验多master写入.读写分离.实 ...
- iptables综述
1 概述 如下图所示,iptables共有Filter,Nat,Mangle和RAW共四个table,每个table还有若干个chain,每个chain中还包含若干个rule 1.1 Filter t ...
- JavaScript中的作用域以及this变量
原文:Scope and this in JavaScript 今天我想简单讨论下关于JavaScript的作用域和this变量."作用域"的概念就是说.我们的代码能够从哪里去訪问 ...
- gb28181的SPVMN测试环境搭建以及设备端和服务器的具体实现
1.GB/T28181开发1之SPVMN(1.0.0.1)环境搭建 https://blog.csdn.net/hiwubihe/article/details/82910685 2.SPVMN 视频 ...
- laravel常用的artisan命令
转载来源链接: https://blog.csdn.net/jiandanokok/article/details/72897682 全局篇 查看artisan命令 php artisan php a ...
- nmon 命令
nmon 命令 用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds >] [ -c < ...
- Eclipse中没有javax.servlet和javax.servlet.http包的处理办法
使用Eclips开发JSP也需要这两个包:javax.servlet和javax.servlet.http:若提示没有javax.servlet包则安装如下处理办法解决: 如果你装了Tomacat,那 ...
- PAT 1090 Highest Price in Supply Chain[较简单]
1090 Highest Price in Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors ...
- (2)R中的数据类型和数据结构
R中的数据结构主要面向<线性代数>中的一些概念,如向量.矩阵等.值得注意的是,R中其实没有简单数据(数值型.逻辑型.字符型等),对于简单类型会自动看做长度为1的向量.比如: > b= ...