-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel_Order_Traversal.cpp
56 lines (46 loc) · 1.1 KB
/
Level_Order_Traversal.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
#include <bits/stdc++.h>
/************************************************************
Following is the BinaryTreeNode class structure
template <typename T>
class BinaryTreeNode {
public:
T val;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T val) {
this->val = val;
left = NULL;
right = NULL;
}
};
************************************************************/
void getLevelOrder(BinaryTreeNode<int> *root, vector<int> &ans)
{
if (root == NULL)
{
return;
}
queue<BinaryTreeNode<int> *> q;
q.push(root);
while (!q.empty())
{
BinaryTreeNode<int> *front = q.front();
q.pop();
ans.push_back(front->val);
if (front->left != NULL)
{
q.push(front->left);
}
if (front->right != NULL)
{
q.push(front->right);
}
}
}
vector<int> getLevelOrder(BinaryTreeNode<int> *root)
{
// Write your code here.
vector<int> ans;
getLevelOrder(root, ans);
return ans;
}