Recover Binary Search Tree [leetcode]

本题是在中序遍历的基础上,找不合规范(不是递增)的树节点对,然后交换

首先看两种序列:

1. 1 3 2 4=>应该交换3和2

2. 4 3 2 1=>应交换4和1

这两种序列对应了不符合条件的BST的中序遍历序列的所有可能性---两个节点中序遍历相邻/不相邻

如果我们用一个数组swap保存所有中序遍历不递增的结果,那么这个数组只可能是2或者4的大小

而我们交换数组中节点对内容,只需交换第一个和最后一个树节点对的内容

    vector<TreeNode *> swap;
    TreeNode * pre;
    void recoverTree(TreeNode *root)
    {
        pre = new TreeNode(-999999);
        swap = vector<TreeNode *>();
        inorder(root);
        if (swap.size() > 1)
        {
            int val = swap[0]->val;
            swap[0]->val = swap[swap.size() - 1]->val;
            swap[swap.size() - 1]->val = val;
        }
    }

    void inorder(TreeNode * root)
    {
        if (root == NULL) return;
        inorder(root->left);
        if (pre->val > root->val)
        {
            swap.push_back(pre);
            swap.push_back(root);
        }
        pre = root;
        inorder(root->right);
    }

由于只交换第一个和最后一个树节点内容,我们可以只保存第一个和最后一个Node

if(!first) first = pre;

last = root;

替换

swap.push_back(pre); swap.push_back(root);

代码如下:

TreeNode * first, * last;
    TreeNode * pre;
    void recoverTree(TreeNode *root)
    {
        pre = new TreeNode(-999999);
        first = last = NULL;
        inorder(root);
        if (first)
        {
            int val = first->val;
            first->val = last->val;
            last->val = val;
        }
    }

    void inorder(TreeNode * root)
    {
        if (root == NULL) return;
        inorder(root->left);
        if (pre->val > root->val)
        {
            if (!first) first = pre;
            last = root;
        }
        pre = root;
        inorder(root->right);
    }
时间: 2024-08-01 13:19:12

Recover Binary Search Tree [leetcode]的相关文章

Recover Binary Search Tree leetcode java

题目: 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: 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

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 之 Recover Binary Search Tree

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? c

LeetCode: Recover Binary Search Tree [099]

[题目] 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

【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 -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

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]Recover Binary Search Tree @ Python

原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树. 算法一:思路很简单,一颗二叉查