剑指offer---从上往下打印二叉树

/*
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) 
    {
        queue<TreeNode*> Q;
        vector<int> result;
        if (root == NULL) return result;
        Q.push(root);
        while (!Q.empty())
        {
            TreeNode* p = Q.front();
            result.push_back(p->val);
            Q.pop();
            if (p->left != NULL)
            {
                Q.push(p->left);
            }
            if (p->right != NULL)
            {
                Q.push(p->right);
            }

        }
        return result;
        

    }
};

 

posted @ 2017-08-06 21:55  双马尾是老公的方向盘  阅读(143)  评论(0编辑  收藏  举报