When seeing this problem,
which let us find something in each tree row,
I thought I could solve this problem by using "Level-order Traversal".
The code is as follows.
The Runtime of the program is 13 ms.
The length of the code is 630Bytes.
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> v;
if (root==NULL) return v;
queue<TreeNode*> qe;
int i,size,max;
qe.push(root);
while (!qe.empty()) {
size=qe.size();
for (i=0, max=qe.front()->val; i<size; i++) {
TreeNode* p = qe.front();
qe.pop();
if (p->val>max)
max=p->val;
if (p->left) qe.push(p->left);
if (p->right) qe.push(p->right);
}
v.push_back(max);
}
return v;
}
};