Largest BST Subtree(Leetcode 333)

這道題有兩種解法。一種top down O(nlong(n)), 另外一種bottom up O(n)。

For bottom up approach, (重點(diǎn)是) in order for tree to be valid, root value needs to be greater than the max of left subtree, and the min of right subtree.

class Solution {
    
    struct ResultSet {
        bool isTree;
        int min_value, max_value;
        int max_cnt;
        ResultSet(bool b, int mi, int mx) : isTree(b), min_value(mi), max_value(mx), max_cnt(0) {}
    };
    
public:
    
    ResultSet FindTree(TreeNode * root){
        if(!root){
            return ResultSet(true, INT_MAX, INT_MIN);
        }
        ResultSet rt(true, 0, 0);
        
        
        ResultSet left = FindTree(root->left);
        ResultSet right = FindTree(root->right);
        
        rt.min_value = min(root->val, min(left.min_value, right.min_value));
        rt.max_value = max(root->val, max(left.max_value, right.max_value));
        
        if(left.isTree && right.isTree) {
            if(root->val > left.max_value && root->val < right.min_value) {
                rt.max_cnt = max(rt.max_cnt, left.max_cnt + right.max_cnt + 1);
                return rt;
            }
        }
        rt.isTree = false;
        rt.max_cnt = max(left.max_cnt, right.max_cnt);
        return rt;
    }
    
    int largestBSTSubtree(TreeNode* root) {
        if(!root) {
            return 0;
        }
        
        ResultSet rs = FindTree(root);
        return rs.max_cnt;
    }
};

其中,最大值直接取左中右的max,最小值直接取左中右的min,并不需要想太多。

        ret.max_value = max(root->val, max(left.max_value, right.max_value));
        ret.min_value = min(root->val, min(left.min_value, right.min_value));

Top down approach 有兩種,一種是top down+ top down, 另外一種是top down + bottom up,兩種區(qū)別在于find_tree utility function.

class Solution {
public:
    
    void FindTree_util(TreeNode *root, TreeNode *large, TreeNode *small, int &cur) {
        if(!root) return;
        if(large && large->val <= root->val) {
            cur = -1;
            return;
        }                                                                          
        else if(small && small->val >= root->val) {
            cur = -1;
            return;
        }
        
        cur = cur + 1;
        FindTree_util(root->left, root, small, cur);
        FindTree_util(root->right, large, root, cur);
    }
    
    void FindTree(TreeNode* root, int &max_ret) {
        if(!root) return;
        int cur = 0;
        FindTree_util(root, NULL, NULL, cur);
        if(cur != -1){
            max_ret = max(max_ret, cur);
        }
        FindTree(root->left, max_ret);
        FindTree(root->right, max_ret);
    }
    
    int largestBSTSubtree(TreeNode* root) {
        if(!root) {
            return 0;
        }
        
        int max_ret = 0;
        FindTree(root, max_ret);
        return max_ret;
    }
};
class Solution {
public:
    
    int FindTree_util(TreeNode *root, TreeNode *large, TreeNode *small) {
        if(!root) return 0;
        if(large && large->val <= root->val) {
            return -1;
        }                                                                          
        else if(small && small->val >= root->val) {
            return -1;
        }
        
        int left_value = FindTree_util(root->left, root, small);
        if(left_value == -1) {
            return -1;
        }
        
        int right_value = FindTree_util(root->right, large, root);
        if(right_value == -1) {
            return -1;
        }
        
        return left_value + right_value + 1;
    }
    
    void FindTree(TreeNode* root, int &max_ret) {
        if(!root) return;
        int ret = FindTree_util(root, NULL, NULL);
        if(ret > max_ret) {
            max_ret = ret;
        }
        FindTree(root->left, max_ret);
        FindTree(root->right, max_ret);
    }
    
    int largestBSTSubtree(TreeNode* root) {
        if(!root) return 0;
        int max_ret = 0;
        FindTree(root, max_ret);
        return max_ret;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容