一开始没有写(1)(2)判断语句,提交答案出错
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> result;
if(root == NULL)//(1)重要
return result;//(2)
result.push_back(root->val);
queue<TreeNode*> Que;
Que.push(root);
while(!Que.empty()){
TreeNode* TNode = Que.front();
Que.pop();
if(TNode->left){
Que.push(TNode->left);
result.push_back(TNode->left->val);
}
if(TNode->right){
Que.push(TNode->right);
result.push_back(TNode->right->val);
}
}
return result;
}
};