-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
62 lines (51 loc) · 922 Bytes
/
binary_tree.cpp
File metadata and controls
62 lines (51 loc) · 922 Bytes
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
/**
*
*
1
/ \
2 3
/ \
4 5
*
*/
#include <iostream>
#include <queue>
using namespace std;
struct treeNode{
struct treeNode *left;
int data;
struct treeNode *right;
};
struct treeNode *getNode(int data)
{
struct treeNode* tmp = new treeNode();
tmp -> data = data;
tmp ->left = NULL;
tmp ->right = NULL;
return tmp;
}
void levelOrder(struct treeNode *root)
{
if(root == NULL) return;
queue<treeNode *> q;
q.push(root);
cout<<"Binary Tree Nodes using Level Order Traversal :";
while(!q.empty())
{
treeNode *curr = q.front();
cout<<curr->data<<" ";
if(curr->left!=NULL) q.push(curr->left);
if(curr->right!=NULL) q.push(curr->right);
q.pop();
}
}
int main() {
struct treeNode *root = getNode(1);
root->left = getNode(2);
root->right = getNode(3);
root->left->left = getNode(4);
root->left->right = getNode(5);
levelOrder(root);
cout<<"\n";
return 0;
}