forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel order traversal of binary tree.cpp
65 lines (51 loc) · 1.44 KB
/
level order traversal of binary tree.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
// Binary Tree is a tree in which each nodes can have atmost two childrens
//Level order traversal of a tree is breadth first traversal for a tree
// 1
// / \
// 2 3
// / \
// 4 5
// here level order traversal is -- 1 2 3 4 5
//It can be implemented using Recursion but time complexity will be O(n^2)
// so optimal approach is by using queue data structure
// code
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node*left;
struct node*right;
// constructor
node(int val){
data=val;
left=right=NULL;
}
};
void levelorder(struct node*root){
queue<struct node*> q; // creating an empty queue
q.push(root); // enqueue root
if(root==NULL) return; // base case
while(!q.empty()){
struct node*curr=q.front();
q.pop();
cout<<curr->data<<" "; // printing front of queue
if(curr->left!=NULL) q.push(curr->left); // enqueue left child
if (curr->right!=NULL) q.push(curr->right); // enqueue right child
}
return;
}
// driver program
int main()
{
struct node*root= new node(1);
root->left= new node(2);
root->right=new node(7);
root->left->left=new node(3);
root->left->right= new node(4);
root->left->right->left= new node(5);
root->left->right->right= new node(6);
levelorder(root);
return 0;
}
// time complexity ---O(n) where n is number of nodes in a tree
// space complexity ---O(n)