题目

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);
} };

GitHub测试程序源码

LeetCode(99) Recover Binary Search Tree的更多相关文章

  1. 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 ...

  2. 【LeetCode 99】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  4. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  5. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

  6. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

  9. 【LeetCode】99. Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

随机推荐

  1. BZOJ1108(思路)

    题目本质:因为只能往南和往东走所以不管怎么组合方案结果都是一样的Orz……我太菜了想不到嘤嘤嘤 #pragma comment(linker, "/STACK:1024000000,1024 ...

  2. (动态规划)UVA-11400:Lighting System Design

    You are given the task to design a lighting system for a huge conference hall. After doing a lot of ...

  3. bzoj3583 杰杰的女性朋友 || bzoj4362 Graph

    http://210.33.19.103/problem/2174 很显然是矩阵快速幂的题,设有in和ou矩阵,设in矩阵的转置为in' 显然可以直接暴力求出任意两点间走一步路径条数,然后求其d次幂, ...

  4. 098 Validate Binary Search Tree 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义:    左子树只包含小于当前节点的数.    右子树只包含大于当前节点的数.    所有子树自身必须也是二叉搜索树.示例 1 ...

  5. DTO和ViewModel的区别

    Data Transfer Object 数据传输对象 ViewModel 视图实体(我们在新建MVC项目是会发现Model文件夹下会有一些ViewModel实体) 简单的理解一下两者之间的区别,举个 ...

  6. SQL Server 查看列,添加列,修改列,删除列

    查看表:exec sp_help 表名 查看列: exec sp_columns 表名 查看列:select * from information_schema.columns where table ...

  7. Nodejs chrome 调试node-inspector

    1.下载扩展: 全局安装 npm install -g node-inspector 2.开启debug调试: node --debug[=port] filename (默认端口5858)node ...

  8. @RequestParam和@ResponseBody注解的区别(转)

    @RequestParam 用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Http协议中,如果不指定Content-Type, ...

  9. Java编程基础-运算符

    Java中的运算符大致分为:算术运算符.赋值运算符.关系运算符.逻辑运算符和位运算符五类. (1).算术运算符:+  -  *  /  %  ++  -- (2).赋值运算符:=  +=  -=  * ...

  10. html5.0学习记录(一)——可拖动视频播放器

    最近自己在重新学习html5新特性,了解到有视频标签和拖动标签,于是自己用这两个特性写了一个小demo,主要功能就是可以通过拖动视频来直接播放.效果图如下: 页面使用了<video>标签和 ...