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); } };
原文地址:https://www.cnblogs.com/AndrewGhost/p/10384559.html
时间: 2024-11-01 21:45:17