-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut116.c++
140 lines (119 loc) · 3.46 KB
/
tut116.c++
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*Bottom View of Binary Tree -> Given a binary tree, print the bottom view from left to right.
A node is included in bottom view if it can be seen when we look at the tree from bottom.
20
/ \
8 22
/ \ \
5 3 25
/ \
10 14
For the above tree, the bottom view is 5 10 3 14 25.
If there are multiple bottom-most nodes for a horizontal distance from root, then print the later one in level traversal. For example, in the below diagram, 3 and 4 are both the bottommost nodes at horizontal distance 0, we need to print 4.
20
/ \
8 22
/ \ / \
5 3 4 25
/ \
10 14
For the above tree the output should be 5 10 4 14 25.
Example 1:
Input:
1
/ \
3 2
Output: 3 1 2
Explanation:
First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.Thus nodes of the binary tree will be printed as such 3 1 2.
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 40 20 60 30
*/
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
};
Node *createNode(int data)
{
// Creating a node pointer
Node *temp;
// Allocating memory in the heap
temp = (struct Node *)malloc(sizeof(struct Node));
// Set the data
temp->data = data;
// Set the left and right children to NULL
temp->left = NULL;
temp->right = NULL;
// Finally return the created node
return temp;
}
// hd as horizontal distance
// Horizontal distance of the root = 0.
// Horizontal distance of a left child = horizontal distance of its parent - 1.
// Horizontal distance of a right child = horizontal distance of its parent + 1.
void bottomViewFind(Node *root, int height, int hd, map<int /*hd of root*/, pair<int /*root data*/, int /*level*/>> &m)
{
if (root == NULL)
{
return;
}
if (m.find(hd) == m.end())
{
m[hd] = make_pair(root->data, height); // height represents level
}
else
{
pair<int, int> p = (m.find(hd))->second;
// for bottom view -> height>=p.second
if (height >= p.second)
{
m.erase(hd);
m[hd] = make_pair(root->data, height);
}
}
// Horizontal distance of a left child = horizontal distance of its parent - 1.
bottomViewFind(root->left, height + 1, hd - 1, m);
// Horizontal distance of a right child = horizontal distance of its parent + 1.
bottomViewFind(root->right, height + 1, hd + 1, m);
}
vector<int> bottomView(Node *root, vector<int> &v)
{
map<int, pair<int, int>> m;
bottomViewFind(root, 0, 0, m);
// By default, a Map in C++ is sorted in increasing order based on its key.
for (map<int, pair<int, int>>::iterator it = m.begin(); it != m.end(); it++)
{
pair<int, int> p = it->second;
v.push_back(p.first);
}
return v;
}
int main()
{
Node *root = createNode(20);
root->left = createNode(8);
root->left->left = createNode(5);
root->left->right = createNode(3);
root->left->right->left = createNode(10);
root->right = createNode(22);
root->right->left = createNode(4);
root->right->left->right = createNode(14);
root->right->right = createNode(25);
vector<int> v;
v = bottomView(root, v);
cout << "Bottom view will be: ";
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
return 0;
}