-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_PrintBinaryTreeFromTopToBottom.cpp
More file actions
61 lines (55 loc) · 1.63 KB
/
Copy path23_PrintBinaryTreeFromTopToBottom.cpp
File metadata and controls
61 lines (55 loc) · 1.63 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <queue>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
void PrintFromTopToBottom(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
queue<BinaryTreeNode*> q;
q.push(pRoot);
while (!q.empty())
{
BinaryTreeNode* u = q.front();
q.pop();
cout << u->m_nValue << " ";
if (u->m_pLeft)
q.push(u->m_pLeft);
if (u->m_pRight)
q.push(u->m_pRight);
}
cout << endl;
}
}
int main()
{
BinaryTreeNode* root1 = new BinaryTreeNode;
root1->m_nValue = 8;
root1->m_pLeft = new BinaryTreeNode;
root1->m_pLeft->m_nValue = 6;
root1->m_pLeft->m_pLeft = new BinaryTreeNode;
root1->m_pLeft->m_pLeft->m_nValue = 5;
root1->m_pLeft->m_pLeft->m_pLeft = NULL;
root1->m_pLeft->m_pLeft->m_pRight = NULL;
root1->m_pLeft->m_pRight = new BinaryTreeNode;
root1->m_pLeft->m_pRight->m_nValue = 7;
root1->m_pLeft->m_pRight->m_pLeft = NULL;
root1->m_pLeft->m_pRight->m_pRight = NULL;
root1->m_pRight = new BinaryTreeNode;
root1->m_pRight->m_nValue = 10;
root1->m_pRight->m_pLeft = new BinaryTreeNode;
root1->m_pRight->m_pLeft->m_nValue = 9;
root1->m_pRight->m_pLeft->m_pLeft = NULL;
root1->m_pRight->m_pLeft->m_pRight = NULL;
root1->m_pRight->m_pRight = new BinaryTreeNode;
root1->m_pRight->m_pRight->m_nValue = 11;
root1->m_pRight->m_pRight->m_pLeft = NULL;
root1->m_pRight->m_pRight->m_pRight = NULL;
PrintFromTopToBottom(root1);
return 0;
}