leetcode || 99、Recover Binary Search Tree

problem:

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?

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

Hide Tags

Tree Depth-first
Search

题意,一颗搜索二叉树,将其中的两个节点交换,找出这个两节点 ,恢复搜索二叉树

thinking:

(1)空间复杂度O(N)的算法最简单:

使用map<int ,TreeNode *>结构可以不破坏原来二叉树的结构

中序遍历二叉树,将数值保存到数组a,同时将每一个数值对应的结点指针保存到map<int ,TreeNode *>,增序排序得到数组b。a与b对比,在a中找到位置不同的元素x、y

在map<int ,TreeNode *>中找到x、y对应的结点指针,交换val值即可

(2)空间复杂度为O(1)的算法:参考http://www.cnblogs.com/remlostime/archive/2012/11/19/2777859.html

code:

class Solution {
public:
    void treeWalk(TreeNode* root, TreeNode*& prv, TreeNode*& first, TreeNode*& second)
    {
      if(root==NULL)
         return;
      treeWalk(root->left,prv,first,second);
      if((prv!=NULL)&&(prv->val>root->val)){
          if(first==NULL)
             first=prv;
           second=root;
      }
      prv=root;
      treeWalk(root->right,prv,first,second);
    }

    void recoverTree(TreeNode *root) {
        TreeNode* first=NULL;
        TreeNode* second=NULL;
        TreeNode* prv=NULL;
        treeWalk(root,prv,first,second);
        int tmp=first->val;
        first->val=second->val;
        second->val=tmp;
    }
};
时间: 2024-08-01 20:42:37

leetcode || 99、Recover Binary Search Tree的相关文章

【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. 题意: 一颗二叉搜索树中有2个结点的元素被误换了,要求恢复二叉搜索树的原状. 思路: 中序遍历BST,若发现一次逆序,说明是两个相连结点的误换,直接交换结点的值:若发现两次逆序,则为不相连的两个结点误换,交换错误的结点即可.用一个变量保存先前结点的状态,若发生逆序则记

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

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

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 首先是O(N)空间的方法,用递归: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *ri

leetcode笔记: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? 二. 题目分析 题目的大意是,在二叉排序

leetcode || 98、Validate Binary Search Tree

problem: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

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

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

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

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

LeetCode: Recover Binary Search Tree

LeetCode: 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 s