剑指OFFER 从上往下打印二叉树

剑指OFFER 从上往下打印二叉树

实际上就是树的深搜

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    
    queue<TreeNode* > que;
    
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        if(root == NULL)return vector<int>();
        que.push(root);
        vector<int> res;
        while(que.size()!=0)
        {
            TreeNode* node = que.front();
            que.pop();
            
            res.push_back(node->val);
            
            if(node->left != NULL)
            {
                que.push(node->left);
            }
            if(node->right != NULL)
            {
                que.push(node->right);
            }
        }
        
        return res;
    }
};
posted @ 2020-01-15 19:20  virgil_devil  阅读(89)  评论(0编辑  收藏  举报