码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】501. Find Mode in Binary Search Tree

时间:2019-02-15 17:20:32      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:less   leetcode   etc   binary   ==   solution   else   arch   roo   

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);
    }
};

  

【leetcode】501. Find Mode in Binary Search Tree

标签:less   leetcode   etc   binary   ==   solution   else   arch   roo   

原文地址:https://www.cnblogs.com/AndrewGhost/p/10384559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!