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 ...
随机推荐
- HDU-1263(STL+排序)
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- Codeforces Round #390 (Div. 2) B
Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...
- ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll
ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll “/” ...
- Java编程基础-字符串
在Java语言中,字符串数据实际上由String类所实现的.Java字符串类分为两类:一类是在程序中不会被改变长度的不变字符串:另一类是在程序中会被改变长度的可变字符串.Java环境为了存储和维护这两 ...
- Android 图片在SD卡及包下的存储
public class FileBitmap { /** * 获取sd卡中的bitmap,bitmap可见 * * @param bitmap * 读取bitmap的路径 * @return bit ...
- [翻译] API测试的最佳实践 - 介绍
API测试的最佳实践 - 介绍 在上一篇“是什么让API测试很叼”一文中,我们讨论API与其他形式的软件测试的差异.部分是因为API之间的通信压根就没考虑让你能读懂,纯粹是为了方便计算机之间的交互而设 ...
- vba 时间
Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...
- 关闭windows7/8的自动升级到windows10
办公室的电脑已经有好几台自动升级到windows10了. 由于用着很不习惯都要求改回windows7. 升级了就不支持退回去,只能是全部删除重新安装了,很是麻烦.但是也没有看到哪里有可以关闭自动升级的 ...
- PBI DAX 中GroupBy
平时工作中经常会遇到Group By 的情形,用sql 写group by 很容易,在PBI中可以这样写: SUMMARIZE(表名,GroupBy Key ,"聚合列命名",DI ...
- openstack 存储节点按照报错Device /dev/sdb not found (or ignored by filtering).
root@dell-PowerEdge-T30:~# pvcreate /dev/sdb Device /dev/sdb not found (or ignored by filtering).首页 ...