Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

题意:

  一颗二叉搜索树中有2个结点的元素被误换了,要求恢复二叉搜索树的原状。

思路:

  中序遍历BST,若发现一次逆序,说明是两个相连结点的误换,直接交换结点的值;若发现两次逆序,则为不相连的两个结点误换,交换错误的结点即可。用一个变量保存先前结点的状态,若发生逆序则记录,空间复杂度为常量。

C++:

 /**
* 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: vector<TreeNode* > errs;
TreeNode *preNode; void rec(TreeNode *root)
{
if(root->left != )
rec(root->left); if(preNode != && root->val < preNode->val)
{
errs.push_back(preNode);
errs.push_back(root);
} preNode = root; if(root->right != )
rec(root->right);
} void recoverTree(TreeNode* root) {
if(root == )
return ; preNode = ; rec(root); int temp = , index = ; if(errs.size() == )
index = ; temp = errs[]->val;
errs[]->val = errs[index]->val;
errs[index]->val = temp;
}
};

【LeetCode 99】Recover Binary Search Tree的更多相关文章

  1. 【LeetCode练习题】Recover Binary Search Tree

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

  2. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  3. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  5. 【leetcode】Recover Binary Search Tree

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

  6. 【leetcode刷题笔记】Recover Binary Search Tree

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

  7. LeetCode(99) Recover Binary Search Tree

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

  8. LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

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

  9. LeetCode具体分析 :: Recover Binary Search Tree [Tree]

    Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straigh ...

随机推荐

  1. PCL—低层次视觉—点云分割(基于凹凸性)

    1.图像分割的两条思路 场景分割时机器视觉中的重要任务,尤其对家庭机器人而言,优秀的场景分割算法是实现复杂功能的基础.但是大家搞了几十年也还没搞定——不是我说的,是接下来要介绍的这篇论文说的.图像分割 ...

  2. windows本地无法启动sqlserver服务

    解决方法:进入服务列表后,选择sqlserver服务,右键然后选择属性,然后在登陆选项卡中,选择本地系统帐户,这样就可以启动sqlserver服务了

  3. 车牌识别LPR(八)-- 字符识别

    ​第八篇:字符识别 车牌定位.车牌倾斜校正.车牌字符分割都是为车牌字符识别做的前提工作,这些前提工作直接关系到车牌识别系统的性能.车牌字符识别是车牌识别系统的核心部分,车牌字符识别的准确率是衡量车牌识 ...

  4. STL容器的效率比较

    1.介绍 顺序存储容器 : string.vector.list.deque 关联存储容器:map底层采用的是树型结构,多数使用平衡二叉树实现,查找某一值是常数时间,遍历起来效果也不错, 只是每次插入 ...

  5. POJ 3468 A Simple Problem with Integers(树状数组)

    题目链接:http://poj.org/problem?id=3468 题意:给出一个数列,两种操作:(1)将区间[L,R]的数字统一加上某个值:(2)查询区间[L,R]的数字之和. 思路:数列A,那 ...

  6. 使用netcat进行反弹链接的shellcode

    from:http://morgawr.github.io/hacking/2014/03/29/shellcode-to-reverse-bind-with-netcat/ 这篇文章主要是谈,在远程 ...

  7. rapidxml使用

    以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高... RapidXml Manual: http://rapidxml.sourceforge.net/ ...

  8. jenkins mac slave 设置

    1.在jenkins上增加节点, 2,在mac系统中将ssh的服务打开在偏好设置- 互联网与无线 - 共享中 3,使用mac root用户修改sshd-config的鉴权方式 首先获取到root用户登 ...

  9. HDU 2586 + HDU 4912 最近公共祖先

    先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edg ...

  10. BZOJ2253: [2010 Beijing wc]纸箱堆叠

    题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...