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 ...
随机推荐
- django (四) model模型
models模型 1. models 定义属性 概述 django根据属性的类型确定以下信息 ·当前选择的数据库支持字段的类型 ·渲染管理表单时使用的默认html控件 ·在管理站点最低限度的验证 dj ...
- python 全局变量 局部变量
##全局变量,局部变量#在函数内部可以调用全局变量,不能随意改变全局变量#若要在函数内部改变全局变量,需用关键字global #代码中全局变量都大写,局部变量都小写(非必须,一种规范) P = &qu ...
- CSS入门使用
声明标签 HTML <!DOCTYPE> 内链样式表 <body style="background-color:green;margin:0;padding:0;&quo ...
- POJ 1458 Common Subsequence DP
http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长 ...
- Linux--NiaoGe-Service-08(路由)
路由 Linux系统下的路由表是由小到大排列的,即C类地址-->B类地址-->A类地址-->0.0.0.0(默认路由). Linux系统中使用route命令查看路由表 [root@w ...
- spring boot使用AbstractXlsView导出excel
一.maven依赖jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi ...
- CAD 安装时出现.net frameword 3.5安装不上的问题
右击---我的电脑---功能---.net framework 3.5 ---勾选---安装,然后再进行安装CAD即可
- Emacs Org-mode中英文字体设置
Emacs Org-mode中英文字体设置 Table of Contents 1. 缺省字体存在的问题 2. 解决方法 2.1. 环境说明 2.2. 思路和方法 2.3. emacs设置代码 2.4 ...
- git代理设置方法
客户公司办公,上外网需要代理,临时查一下资料,记录一下: 1.设置代理: git config --global http.proxy http://IP:Port 2.代理设置完成后,查看设置是否生 ...
- SVN上传文件过程中出现错误“不知道这样的主机”
在虚拟机中安装完成VisualSVN Server,并且在本地客户端也安装好了TortoiseSVN,在上传文件到服务器的过程中出现错误“不知道这样的主机”,如下图: 地址https://admin- ...