-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23从上往下打印二叉树.cpp
42 lines (39 loc) · 1.07 KB
/
23从上往下打印二叉树.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
37
38
39
40
41
42
//23
// 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
// 思路:
// 层次遍历,采用队列!
/*
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)
{
if (root == nullptr)
return vector<int>();
vector<int> output;
queue<TreeNode*> hyQueue;
hyQueue.push(root);
TreeNode *pNode = nullptr; //队头
while(!hyQueue.empty())//建立循环机制,这样的话,就是左右子树先入队,它才出队
{
pNode = hyQueue.front();//(跟堆栈一样 .top() )
output.push_back(pNode->val);
if(pNode->left)
hyQueue.push(pNode->left);//左子树 入队
if(pNode->right)
hyQueue.push(pNode->right);//右子树 入队
hyQueue.pop();//“根节点”出队
}
return output;
}
};
//**注意区分
// vector<int> A; 是 .push_back();
// stack<int> B; queue<TreeNode* > C; 是 .push();