leetcode[99] Recover Binary Search Tree

题目:二叉树中有两个节点对换了值,恢复他们。

思路:
因为中序遍历是有序的,如果中序遍历后的数组出现乱序,说明就是交换的。从前往后,第一次乱序的第一个,后最后一次乱序的后一个,然后把这两个值对换就好了。

想了个非常挫的办法。

先中序遍历Binary Tree Inorder Traversal,然后在数组中找到两个值,然后再中序遍历找到那两个值,交换一下。虽然AC了,但不忍直视啊。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void inorder(TreeNode *root, vector<int> &perm) // 中序遍历得到数组
    {
        if (root == NULL) return ;
        stack<TreeNode *> sta;

        sta.push(root);
        TreeNode *p = root -> left, *cen;

        while(!sta.empty())
        {
            if (p)
            {
                sta.push(p);
                p = p -> left;
            }
            else
            {
                cen = sta.top();
                sta.pop();
                perm.push_back(cen -> val);
                if (cen -> right)
                {
                    sta.push(cen -> right);
                    p = cen -> right -> left;
                }
            }
        }
    }
        void inorderC(TreeNode *root,int val1, int val2) // 中序遍历将对应值修改
    {
        if (root == NULL) return ;
        stack<TreeNode *> sta;

        sta.push(root);
        TreeNode *p = root -> left, *cen;

        while(!sta.empty())
        {
            if (p)
            {
                sta.push(p);
                p = p -> left;
            }
            else
            {
                cen = sta.top();
                sta.pop();
                if(cen -> val == val1)
                    cen -> val = val2;
                else if(cen -> val == val2)
                    cen -> val = val1;
                if (cen -> right)
                {
                    sta.push(cen -> right);
                    p = cen -> right -> left;
                }
            }
        }
    }
    void recoverTree(TreeNode *root)
    {
        if (!root) return ;
        vector<int> perm;
        inorder(root, perm);
        int val1, val2;
        bool flag = true;
        for (int i = 0; i < perm.size(); ++i)
        {
            if (flag && i + 1 < perm.size() && perm[i] > perm[i+1]) // 第一次比后一个大的数
            {
                val1 = perm[i];
                flag = false;
            }
            if (i - 1 >= 0 && perm[i] < perm[i-1]) // 最后一个比前面小的数
                val2 = perm[i];
        }
        inorderC(root, val1, val2);
    }
};

然后就想到改进,在一次中序遍历中就记录两个节点,然后交换值。

时间: 2024-10-22 03:29:11

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

leetcode 99 Recover Binary Search Tree ----- 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] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

19.3.2 [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. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

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

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 OJ 99. 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? Subscribe to see which compan

99. 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? 解题思路:本题主要是考查了Morris Traversal