C++:从上到下打印二叉树

从上往下打印二叉树

http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701

class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> vec;
if(root == NULL) {
return vec;
}
vector<TreeNode> q;
q.push_back(root);
while(!q.empty()) {
TreeNode
node = q.front();
q.erase(q.begin());
vec.push_back(node->val);
if(node->left)q.push_back(node->left);
if(node->right)q.push_back(node->right);
}
return vec;
}
};</int></int>

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务