LeetCode(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?
分析
给定一颗二叉排序树,它的两个节点被交换,要求在常数空间复杂度内把它恢复为原来的二叉排序树。
方法一:我们知道二叉排序树的中序遍历序列为递增的有序序列,首先,中序遍历该二叉树,存储元素序列,然后循环查找元素序列中逆序的两个节点元素,交换之,还原成二叉树即可。该方法需要O(n)的空间,n为节点个数,不满足要求。
查找资料,得到方法二:
递归中序遍历二叉树,设置一个pre指针,记录当前节点中序遍历时的前节点,如果当前节点大于pre节点的值,说明需要调整次序。
有一个技巧是如果遍历整个序列过程中只出现了一次次序错误,说明就是这两个相邻节点需要被交换。如果出现了两次次序错误,那就需要交换这两个节点。
AC代码
/**
* 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:
//节点p,q为两个逆序节点 , pre为遍历当前root节点的前驱
TreeNode *p = NULL, *q = NULL, *pre = NULL;
void recoverTree(TreeNode* root) {
if (!root)
return;
//中序遍历二叉树,查找需要逆序节点
InOrder(root);
if (p && q)
{
int tmp = p->val;
p->val = q->val;
q->val = tmp;
}//if
}
//中序遍历二叉树root,同时查找树中逆序节点p , q
void InOrder(TreeNode *root)
{
if (!root)
return;
if (root->left)
InOrder(root->left);
if (pre != NULL && pre->val > root->val)
{
if (!p)
p = pre;
q = root;
}//if
pre = root;
if (root->right)
InOrder(root->right);
}
};
LeetCode(99) Recover Binary Search Tree的更多相关文章
- LeetCode(98) Validate Binary Search Tree
题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...
- 【LeetCode 99】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- 【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 ...
随机推荐
- 使用Hexo 搭建自己的博客
使用Hexo 搭建自己的博客 最近一直都想着如何去搭建属于自己的博客,有空的时候就写写文章什么的. 本人对该博客系统的要求是: 博文的编写要采用现在流行的MarkDown来进行编写. 本人还不想去注册 ...
- how browser supports https
1. pre-installed certificate authorities 2. ssl/tls encription ssl/tls handshake flow: 1. exchange d ...
- python+selenium 页面中存在选项卡时,获取页面内容的小技巧
最近用selenium读取页面内容时,遇到包含选项卡的页面,由于选项卡多由js加载其中的内容,所以在网址打开时只能获取到默认显示的选项卡中的内容,而tab2.tab3等等都需要傻傻的点击一下才会获取到 ...
- Executor等系列概念介绍
这里对几个常见的的名词进行介绍 Executor 这是个接口,只声明了一个方法—— public interface Executor { void execute(Runnable command) ...
- python学习之序列化
序列化:不同编程语言之间传递对象需要序列化成标准格式,有XML /JSON,json格式化为字符串,UTF-8编码,速度快,切实标准格式.JSON 和 Python内置的数据类型对应如下: JSON ...
- 如何正确在IDEA 里非maven或非SBT构建的项目中引入lib的jar包(图文详解)
以下是我,手动的一个项目 假设,大家,还需要导入 导入spark的jar包:是安装主目录下的jars所有jar包和examples/jars包.
- CentOS远程监控
近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...
- SpringBoot 2.x (10):整合Redis
Redis部署到阿里云: 下载redis源码,编译,允许远程访问的配置 阿里云安全组设置: SSH连过去: wget http://download.redis.io/releases/redis-4 ...
- 简单的UDP程序
接受端: package com.dcz.udp; import java.io.IOException; import java.net.DatagramPacket; import java.ne ...
- JavaScript 跨域请求
1.最简单通用的做法就是 反向代理 通过nginx搭建一个反向代理服务器,通过将跨域的请求配置成转发:此方法适用于动静分离时,很多跨域请求的情况下: server { listen 8 ...