-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevel_Order_Traversal.cpp
76 lines (62 loc) · 1.46 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Problem Statement:
-----------------
Given a binary tree, find its level order traversal. Level order traversal of a tree is breadth-first traversal for the tree.
Example 1:
---------
Input:
1
/ \
3 2
Output:1 3 2
Example 2:
---------
Input:
10
/ \
20 30
/ \
40 60
Output:10 20 30 40 60 N N
Your Task: You don't have to take any input. Complete the function levelOrder() that takes the root node as input parameter and
returns a list of integers containing the level order traversal of the given Binary Tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
*/
// Link --> https://practice.geeksforgeeks.org/problems/level-order-traversal/1#
// Code:
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
vector <int> result;
class Solution
{
public:
//Function to return the level order traversal of a tree.
vector <int> levelOrder(Node* node)
{
result.clear();
Node *temp;
queue <Node*> q;
q.push(node);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
result.push_back(temp->data);
}
return result;
}
};