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. chmod命令详细用法

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  2. 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】02-第一个C语言程序

    前言 前面已经唠叨了这么多理论知识,从这讲开始,就要通过接触代码来学习C语言的语法.学习任何一门语言,首先要掌握的肯定是语法.学习C语言语法的目的:就是能够利用C语言编写程序,然后运行程序跟硬件(计算 ...

  3. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  4. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  5. 3.Spring-用反射模拟IoC

    1.BeanFactory.java package com.jike.spring.chapter03.reflect; import java.io.InputStream; import jav ...

  6. NSMutableArray 排序

    NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"1",@"3",@"2" ...

  7. tcpdump抓SQL

    前言:假设如果有个服务器几十个链接突然达到上千个链接,show processlist,general_log,还有慢查询日志这些都不能用,你怎么把这些链接过来的SQL情况了解清楚,如果你觉得那些好用 ...

  8. NDK(12)Jni常用函数

    参考官方文档 http://docs.oracle.com/javase/7/docs/technotes/guides/jni/ http://docs.oracle.com/javase/7/do ...

  9. JS中字符串拼装 单双引号的处理 字符转义

    js中可能会用到动态追加元素,可能数据也是从后台传过来的,当然有两种思路, 1.在后台拼装好直接返回; 2.在前台js里面拼装, 如果拼装大量的html时可能单双引号就容易出问题;那么如何解决呢?最近 ...

  10. C++STL之整理算法

    这里主要介绍颠倒.旋转.随机排列和分类4中常见的整理算法 1.颠倒(反转) void reverse(_BidIt _First, _BidIt _Last) _OutIt reverse_copy( ...