leetcode501. Find Mode in Binary Search Tree

Posted 业精于勤荒于嬉,行成于思毁于随

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode501. Find Mode in Binary Search Tree相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    vector<int> modes;
    int maxCnt = 0;
    int curCnt = 0;
    int curNum = 0;
    vector<int> findMode(TreeNode* root) {
       if (!root) {
          return modes;
       }

       curNum = root->val; 
       inOrder(root);
        
       return modes;
    }
    
    void inOrder(TreeNode* root) {
        if (!root) {
            return;
        }
        
        inOrder(root->left);
        
        if (root->val == curNum) {
            curCnt++;
        } else {
            curCnt = 1;
            curNum = root->val;
        }
        
        if (curCnt > maxCnt) {
            // clear all number that have less occurred times
            modes = {};
            modes.push_back(curNum);
            maxCnt = curCnt;
        } else if (curCnt == maxCnt) {
            modes.push_back(curNum);
        }  

        inOrder(root->right);
    }
};

  

以上是关于leetcode501. Find Mode in Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree

leetcode501. Find Mode in Binary Search Tree

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

LeetCode 501. Find Mode in Binary Search Tree(寻找二叉查找树中出现次数最多的值)

501. Find Mode in Binary Search Tree 查找BST中的众数