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_501. Find Mode in Binary Search Tree

501. Find Mode in Binary Search Tree - Easy

leetcode 501. Find Mode in Binary Search Tree

501. Find Mode in Binary Search Tree

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

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