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

地址:https://oj.leetcode.com/problems/recover-binary-search-tree/

算法:二叉搜索树中有两个节点被错误的交换了,要求找到这两个节点,并把二者交换回来,要求用常数的空间。首先,使用中序遍历来搜索二叉树,当找到一个树比他的前趋还要小的时候,说明这个点是第一个错误点,即为A,然后接下去找第二个错误点,第二个错误点应该在第一个大于A点值的前面一个节点,记为B,接下去交换A,B节点即可。注意处理B点为最后一个节点时的情况。代码:

/**
 * 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 recoverTree(TreeNode *root) {
        if(!root)   return ;
        stack<TreeNode *> stk;
        TreeNode *p = root;
        while(p){
            stk.push(p);
            p = p->left;
        }
        TreeNode *pre = NULL;
        p = NULL;
        TreeNode *swapped_node1 = NULL;
        bool is_swapped = false;
        bool find_node = false;
        while(!stk.empty()){
            pre = p;
            p = stk.top();
            stk.pop();
            if(!find_node && pre && pre->val > p->val){
                swapped_node1 = pre;
                find_node = true;
            }
            if(find_node && p->val >= swapped_node1->val){
                if(pre){
                    int temp = swapped_node1->val;
                    swapped_node1->val = pre->val;
                    pre->val = temp;
                    is_swapped = true;
                    break;
                }
            }
            if(p->right){
                TreeNode *q = p->right;
                while(q){
                    stk.push(q);
                    q = q->left;
                }
            }
        }
        if(!is_swapped){
            int temp = swapped_node1->val;
            swapped_node1->val = p->val;
            p->val = temp;
        }
        return ;
    }
};
时间: 2024-11-08 21:24:49

LeetCode: Recover Binary Search Tree的相关文章

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 @ 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. 解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树. 算法一:思路很简单,一颗二叉查

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——二查搜索树中两个节点错误

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 复原二叉搜索树

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 [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 BST 中序遍历

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

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? 题解: 解决方法是利用中序遍历找顺序不对的两个点