-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeTranverse.cpp
131 lines (119 loc) · 2.85 KB
/
treeTranverse.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 本代码使用递归的方法依次实现了前序遍历、中序遍历、后序遍历、层次遍历
//
// 树的形状: 8
// 3 10
// 1 6 null 15
// 各种遍历结果:
// 前序遍历:8 3 1 6 10 15
// 中序遍历:1 3 6 8 10 15
// 后序遍历:1 6 3 15 10 8
// 层次遍历:8 3 10 1 6 15
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL)
{
}
};
void preOrder(TreeNode *pHead)
{
// 先序遍历,根 - 左 - 右
if (pHead == NULL)
return;
cout << pHead->val << " ";
preOrder(pHead->left);
preOrder(pHead->right);
}
void inOrder(TreeNode *pHead)
{
// 中序遍历,左 - 根 - 右
if (pHead == NULL)
return;
inOrder(pHead->left);
cout << pHead->val << " "; // 当前节点在其左右子节点之间访问, 左 - 根 - 右
inOrder(pHead->right);
}
void posOrder(TreeNode *pHead)
{
// 后序遍历,左 - 右 - 根
if (pHead == NULL)
return;
posOrder(pHead->left);
posOrder(pHead->right);
cout << pHead->val << " "; // 当前节点在其左右子节点之后访问,左-右-根
}
// 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
void levelOrder(TreeNode *pHead)
{
if (pHead == NULL)
return;
deque<TreeNode *> dequeTreeNode;
dequeTreeNode.push_back(pHead);
while (!dequeTreeNode.empty())
{
TreeNode *temp = dequeTreeNode.front();
// push val to vector
cout << temp->val << " ";
if (temp->left != NULL)
dequeTreeNode.push_back(temp->left);
if (temp->right != NULL)
dequeTreeNode.push_back(temp->right);
dequeTreeNode.pop_front();
}
}
// 利用层次遍历,每一层计数并处理一次
int TreeDepth(TreeNode *pRoot)
{
if (!pRoot)
return 0;
queue<TreeNode *> qu;
qu.push(pRoot);
int depth = 0;
while (!qu.empty())
{
int size = qu.size();
depth++;
for (int i = 0; i < size; i++)
{
TreeNode *temp = qu.front();
cout << "__" << temp->val;
qu.pop();
if (temp->left != NULL)
qu.push(temp->left);
if (temp->right != NULL)
qu.push(temp->right);
}
cout << endl;
}
return depth;
}
int main()
{
TreeNode a(8);
TreeNode b(3);
TreeNode c(10);
TreeNode d(1);
TreeNode e(6);
TreeNode f(15);
a.left = &b;
a.right = &c;
b.left = &d;
b.right = &e;
c.right = &f;
preOrder(&a);
cout << endl;
inOrder(&a);
cout << endl;
posOrder(&a);
cout << endl;
levelOrder(&a);
cout << endl;
return 0;
}