1. 程式人生 > >[LeetCode] 501. Find Mode in Binary Search Tree

[LeetCode] 501. Find Mode in Binary Search Tree

Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Both the left and right subtrees must also be binary search trees.
For example:
Given BST [1,null,2,2],
return [2].

Note: If a tree has more than one mode, you can return them in any order.
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

解析

找到二叉樹中的眾數。

解法1:inorder(遞迴)

遞迴中序遍歷二叉樹,建立一個雜湊表,儲存一個最大個數。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        int mx = 0;
        unordered_map<int, int> m; 
        inorder(root, m, mx);
        for (auto a : m) {
            if (a.second == mx) {
                res.push_back(a.first);
            }
        }
        return res;
    }
    
    void inorder(TreeNode* root, unordered_map<int, int>& m, int& mx){
        if(!root) return;
        inorder(root->left, m, mx);
        m[root->val] ++;
        mx = max(mx, m[root->val]);
        inorder(root->right, m, mx);
    }
};

解法2:inorder(迭代)

迭代中序遍歷二叉樹,建立一個雜湊表,儲存一個最大個數。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(!root) return {};
        vector<int> res;
        int mx = 0;
        unordered_map<int, int> m; 
        stack<TreeNode*> s;
        TreeNode* p = root;
        while(!s.empty() || p){
            while(p){
                s.push(p);
                p = p->left;
            }
            p = s.top();
            s.pop();
            m[p->val] ++;
            mx = max(mx, m[p->val]);
            p = p->right;
        }
        for (auto a : m) {
            if (a.second == mx) {
                res.push_back(a.first);
            }
        }
        return res;
    }
};

解法3:inorder(遞迴不使用額外空間)

利用二叉樹的中序遍歷有序的特性,對二叉樹遞迴中序遍歷,設定一個pre節點,一個count記錄當前節點次數,當pre不為空時,不是第一個節點,與前一個節點值比較,如果相等則count+1,否則count=1;
當count大於最大次數mx時,清空res,加入當前節點,並將mx=count;pre更新為當前節點p。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        int mx = 0;
        int count = 1;
        TreeNode* pre = NULL;
        inorder(root, pre, count, mx, res);
        return res;
    }
    
    void inorder(TreeNode* p, TreeNode*& pre, int& count,int& mx, vector<int>& res){
        if(!p) return;
        inorder(p->left, pre, count,mx,res);
        if(pre){
            count = (pre->val == p->val) ? count+1:1;
        }
        if(count>=mx){
            if(count>mx) res.clear();
            res.push_back(p->val);
            mx = count;
        }
        pre = p;
        inorder(p->right, pre, count,mx,res);
    }
};

解法4:inorder(迭代不使用額外空間)

迭代中序遍歷,類似於解法3

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(!root) return {};
        vector<int> res;
        int mx = 0;
        int count =1;
        stack<TreeNode*> s;
        TreeNode* p = root;
        TreeNode* pre = NULL;
        while(!s.empty() || p){
            while(p){
                s.push(p);
                p = p->left;
            }
            p = s.top();s.pop();
            if(pre){
                count = (pre->val == p->val) ? count+1:1;
            }
            if(count>=mx){
                if(count>mx) res.clear();
                res.push_back(p->val);
                mx = count;
            }
            pre = p;
            p = p->right;
        }
        return res;
    }
};

參考

http://www.cnblogs.com/grandyang/p/6436150.html