Accepted Solution for Mode


  • 0
    L
    public:
     vector<int> result;
     int max=0;
        
        int counter(TreeNode* root,int num) {
         
            if(!root)
                return 0;
            
            if(root->val==num)
                return 1+counter(root->left,num)+counter(root->right,num);
            
            return counter(root->left,num)+counter(root->right,num);
            
            
        }
        
        
        vector<int> findMode(TreeNode* root) {
            
            int count;
            TreeNode* rootl;
            TreeNode* rootr;
            
            if(!root)
                return result;
            
            count=counter(root,root->val);
            
            if(count>max)
            {
                max=count;
                result.clear();
                result.push_back(root->val);
                
            }
            else if(count==max)
                result.push_back(root->val);
            
            
            if(root->left)
            {
               result=findMode(root->left);
                
            }
            
            
             if(root->right)
              {
                result=findMode(root->right);
             }
            
            
            return result;
            
            
            
        }
    };```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.