-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottom_view_of_binary_tree.cpp
77 lines (67 loc) · 1.52 KB
/
bottom_view_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
66
67
68
69
70
71
72
73
74
75
76
77
// C++ Program to print Bottom View of Binary Tree
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
// Tree node class
struct Node
{
int data; //data of the node
int hd; //horizontal distance of the node
Node *left, *right; //left and right references
// Constructor of tree node
Node(int key)
{
data = key;
hd = INT_MAX;
left = right = NULL;
}
};
// Method that prints the bottom view.
void bottomView(Node *root)
{
queue<Node *> q;
map<int,int> m;
Node *top;
if (root==NULL) {
return;
}
root->hd=0;
m[0]=root->data;
q.push(root);
while(!q.empty()){
top=q.front();
q.pop();
if(top->left)
{
q.push(top->left);
top->left->hd=(top->hd)-1;
m[top->left->hd]=top->left->data;
}
if(top->right)
{
q.push(top->right);
top->right->hd=(top->hd)+1;
m[top->right->hd]=top->right->data;
}
}
for (auto i = m.begin(); i != m.end(); ++i)
cout << i->second << " ";
}
// Driver Code
int main()
{
Node *root = new Node(20);
root->left = new Node(8);
root->right = new Node(22);
root->left->left = new Node(5);
root->left->right = new Node(3);
root->right->left = new Node(4);
root->right->right = new Node(25);
root->left->right->left = new Node(10);
root->left->right->right = new Node(14);
cout << "Bottom view of the given binary tree :\n";
bottomView(root);
return 0;
}