class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int>ans;
int m,size = 1,i = 0;
TreeNode* t;
queue<TreeNode*>q;
if (root)q.push(root), m = root->val;
while (!q.empty()) {
t = q.front();
if (t->val > m)m = t->val;
q.pop(); i++;
if (t->left)q.push(t->left);
if (t->right)q.push(t->right);
if (i == size) {
size = q.size(), ans.push_back(m), i = 0;
if (!q.empty())m = q.front()->val;
}
}
return ans;
}
};
LeetCode 515. 在每个树行中找最大值
最新推荐文章于 2024-07-13 19:27:27 发布