zoukankan      html  css  js  c++  java
  • 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}" means? > read more on how binary tree is serialized on OJ.

    class Solution {
    private:
        vector<TreeNode*> nodes;
    public:
        void recoverTree(TreeNode *root) {
            nodes.clear();
            dfs(root);
            // 1 5 3 4 2 6 7
            // 1 3 2 4
            // 3 2
            int len = nodes.size();
            if (len < 1) return;
            
            int pre = nodes[0]->val;
            int cur = 0;
            int first = -1;
            int second= -1;
            for (int i=1; i<len; i++) {
                cur = nodes[i]->val;
                if (cur < pre) {
                    if (first < 0) {
                        first = i-1;
                    } else {
                        second= i;
                    }
                }
                pre = cur;
            }
            if (second < 0) second = first + 1;
            int t = nodes[first]->val;
            nodes[first]->val = nodes[second]->val;
            nodes[second]->val= t;
        }
        
        void dfs(TreeNode* root) {
            if (root == NULL) return;
            dfs(root->left);
            nodes.push_back(root);
            dfs(root->right);
        }
    };

    先来个"A solution using O(n) space is pretty straight forward"的方法。如果递归调用栈不算额外空间的话,把先序遍历改一下即可,如果算的话...怎么办

    来一个伪常数空间的:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    private:
        vector<TreeNode*> nodes;
        TreeNode* pre;
        TreeNode* first;
        TreeNode* second;
    public:
        void recoverTree(TreeNode *root) {
            nodes.clear();
            pre = first = second = NULL;
            dfs(root);
            // case a. 1 5 3 4 2 6 7
            // case b. 1 3 2 4
            // case c. 3 2
            // case d. 3
            // case e. NULL
            if (second == NULL) return; // case (d,e)
            int t = first->val;
            first->val = second->val;
            second->val= t;
        }
        
        void dfs(TreeNode* root) {
            if (root == NULL) return;
            dfs(root->left);
            visit(root);
            dfs(root->right);
        }
        
        void visit(TreeNode* node) {
            if (pre == NULL) {
                pre = node;
                return;
            }
            if (node->val < pre->val) {
                if (first == NULL) {
                    first = pre;
                    second= node;   // assume swap with node next to pre(case b,c)
                } else {
                    second= node;   // fix above assumption(case a)
                }
            }
            pre = node;
        }
    };

     第二轮:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    private:
        TreeNode* first;
        TreeNode* second;
        TreeNode* last;
    public:
        void recoverTree(TreeNode* root) {
            last = first = second = NULL;
            dfs(root);
            swap(first->val, second->val);
        }
        
        void dfs(TreeNode* root) {
            if (root == NULL) {
                return;
            }
            dfs(root->left);
            
            if (last != NULL) {
                if (last->val > root->val) {
                    if (first == NULL) {
                        first = last;
                    }
                    second = root;
                }
            }
            last = root;
            
            dfs(root->right);
        }
    };
  • 相关阅读:
    C++ 虚函数 、纯虚函数、接口的实用方法和意义
    C++ static成员变量与static成员函数
    C++格式化输出的好东西
    const的用法,特别是用在函数前面与后面的区别!
    SAP CRM WebClient UI cross component跳转的一个具体例子
    如何用ABAP代码读取SAP Business partner的附件数据
    SAP CRM Enterprise Search initial load遇到错误该如何处理
    使用SAP WebIDE给SAP UI5应用添加data source
    使用Selenium自动化测试SAP CRM WebClient UI
    php 对象数组互转
  • 原文地址:https://www.cnblogs.com/lailailai/p/3933958.html
Copyright © 2011-2022 走看看