Skip to content

Commit

Permalink
从上往下打印二叉树
Browse files Browse the repository at this point in the history
层序遍历二叉树
  • Loading branch information
DreamOfFootPrints committed Aug 14, 2016
1 parent fbd753e commit 30ea23c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 9/PrintFromTopToBottom.h
@@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
#include<vector>
#include<queue>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}

};




vector<int> PrintFromTopToBottom(TreeNode *root)
{
typedef TreeNode Node;
vector<int> vec;
if (root == NULL) return vec;

queue<Node*> q;
q.push(root);
while (!q.empty())
{
Node* front = q.front();
q.pop();
vec.push_back(front->val);
if (front->left != NULL) q.push(front->left);
if (front->right != NULL) q.push(front->right);
}
return vec;
}







0 comments on commit 30ea23c

Please sign in to comment.