剑指offer 二叉树的bfs


/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int> res;
        if(root == nullptr) return res;
        queue<TreeNode*> q;
        q.push(root);
        while ( !q.empty() ){
            auto node = q.front();
            res.emplace_back(node->val);
            if(node->left != nullptr) q.push(node->left);
            if(node->right != nullptr) q.push(node->right);
            q.pop();
        }
        return res;
    }
};

posted @ 2018-08-26 17:34  一条图图犬  阅读(181)  评论(0编辑  收藏  举报