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}" means? >
read more on how binary tree is serialized on OJ.

【题意】

给定的二叉搜索树中有两个节点的值错换了,找出这两个节点,恢复二叉搜索树。要求不适用额外的空间。

【思路】

中序遍历二叉树,一棵正常的二叉树中序遍历得到有序的序列,现有两个节点的值的调换了,则肯定是一个较大的值被放到了序列的前段,而较小的值被放到了序列的后段。节点的错换使得序列中出现了s[i-1]>s[i]的情况,如果错换的点正好是相邻的两个数,则s[i-1]>s[i]的情况只出现一次;如果不相邻,则会出现两次,第一次出现是前者为错换的较大值节点,第二次出现时后者为错换的较小值节点。

【代码】

/**
 * 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) {
        stack<TreeNode*> st;
        TreeNode* pointer=root;
        TreeNode* prev=NULL;
        TreeNode* nodeLarge=NULL;
        TreeNode* nodeSmall=NULL;
        while(pointer){st.push(pointer); pointer=pointer->left;}
        while(!st.empty()){
            TreeNode* cur = st.top();
            st.pop();
            if(prev && prev->val > cur->val){
                if(nodeLarge==NULL || prev->val > nodeLarge->val) nodeLarge=prev;
                if(nodeSmall==NULL || cur->val < nodeSmall->val) nodeSmall=cur;
            }
            prev=cur;
            pointer=cur->right;
            while(pointer){st.push(pointer); pointer=pointer->left;}
        }
        //替换两个节点的值
        int temp=nodeLarge->val;
        nodeLarge->val = nodeSmall->val;
        nodeSmall->val = temp;
    }
};

LeetCode: Recover Binary Search Tree [099]

时间: 2024-10-12 16:19:50

LeetCode: Recover Binary Search Tree [099]的相关文章

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

099 Recover Binary Search Tree

099 Recover Binary Search Tree 没有按照要求用 constant space.... 用valid BST 找出两个not in order 的nodes class Solution: def recoverTree(self, root): [fN, sN] = self.valid(root) fN.val, sN.val = sN.val, fN.val def valid(self, root): pre, fN, sN = TreeNode(-10000

【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树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两