Skip to content

Commit 2183111

Browse files
Create bottom_view_binary_tree.cpp
1 parent 24b644d commit 2183111

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

Diff for: bottom_view_binary_tree.cpp

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// { Driver Code Starts
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
#define MAX_HEIGHT 100000
5+
6+
// Tree Node
7+
struct Node
8+
{
9+
int data;
10+
Node* left;
11+
Node* right;
12+
};
13+
14+
// Utility function to create a new Tree Node
15+
Node* newNode(int val)
16+
{
17+
Node* temp = new Node;
18+
temp->data = val;
19+
temp->left = NULL;
20+
temp->right = NULL;
21+
22+
return temp;
23+
}
24+
25+
26+
vector <int> bottomView(Node *root);
27+
28+
// Function to Build Tree
29+
Node* buildTree(string str)
30+
{
31+
// Corner Case
32+
if(str.length() == 0 || str[0] == 'N')
33+
return NULL;
34+
35+
// Creating vector of strings from input
36+
// string after spliting by space
37+
vector<string> ip;
38+
39+
istringstream iss(str);
40+
for(string str; iss >> str; )
41+
ip.push_back(str);
42+
43+
// Create the root of the tree
44+
Node* root = newNode(stoi(ip[0]));
45+
46+
// Push the root to the queue
47+
queue<Node*> queue;
48+
queue.push(root);
49+
50+
// Starting from the second element
51+
int i = 1;
52+
while(!queue.empty() && i < ip.size()) {
53+
54+
// Get and remove the front of the queue
55+
Node* currNode = queue.front();
56+
queue.pop();
57+
58+
// Get the current node's value from the string
59+
string currVal = ip[i];
60+
61+
// If the left child is not null
62+
if(currVal != "N") {
63+
64+
// Create the left child for the current node
65+
currNode->left = newNode(stoi(currVal));
66+
67+
// Push it to the queue
68+
queue.push(currNode->left);
69+
}
70+
71+
// For the right child
72+
i++;
73+
if(i >= ip.size())
74+
break;
75+
currVal = ip[i];
76+
77+
// If the right child is not null
78+
if(currVal != "N") {
79+
80+
// Create the right child for the current node
81+
currNode->right = newNode(stoi(currVal));
82+
83+
// Push it to the queue
84+
queue.push(currNode->right);
85+
}
86+
i++;
87+
}
88+
89+
return root;
90+
}
91+
92+
93+
// } Driver Code Ends
94+
//Function to return a list containing the bottom view of the given tree.
95+
96+
class Solution {
97+
public:
98+
vector <int> bottomView(Node *root) {
99+
// Your Code Here
100+
if(!root) return {};
101+
102+
map<int, int> mp;
103+
queue<pair<Node *, int>> qu;
104+
qu.push({root, 0});
105+
106+
while(!qu.empty()) {
107+
108+
auto temp = qu.front();
109+
Node *current = temp.first;
110+
int hd = temp.second;
111+
qu.pop();
112+
113+
114+
mp[hd] = current->data;
115+
116+
117+
if(current->left)
118+
qu.push({current->left, hd - 1});
119+
120+
if(current->right)
121+
qu.push({current->right, hd + 1});
122+
}
123+
124+
vector<int> result;
125+
for(auto kv : mp) {
126+
result.push_back(kv.second);
127+
}
128+
129+
return result;
130+
}
131+
};
132+
133+
// { Driver Code Starts.
134+
135+
int main() {
136+
int t;
137+
string tc;
138+
getline(cin, tc);
139+
t=stoi(tc);
140+
while(t--)
141+
{
142+
string s ,ch;
143+
getline(cin, s);
144+
Node* root = buildTree(s);
145+
Solution ob;
146+
vector <int> res = ob.bottomView(root);
147+
for (int i : res) cout << i << " ";
148+
cout << endl;
149+
}
150+
return 0;
151+
}
152+
153+
154+
// } Driver Code Ends

0 commit comments

Comments
 (0)