-
Notifications
You must be signed in to change notification settings - Fork 0
/
从上往下打印二叉树.cpp
36 lines (34 loc) · 943 Bytes
/
从上往下打印二叉树.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* ***************************
* 从上往下打印二叉树(举例让抽象具体化)
* 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
* ***************************/
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> result;
if(root==NULL)
return result;
//
queue<TreeNode*> tempQ;
TreeNode *pTemp=root;
tempQ.push(root);
while(!tempQ.empty()){
pTemp = tempQ.front();
tempQ.pop();
result.push_back(pTemp->val);
if(pTemp->left != NULL)
tempQ.push(pTemp->left);
if(pTemp->right != NULL)
tempQ.push(pTemp->right);
}
return result;
}
};