4 out of 5 test cases are showing Run Error.
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
// construct.
node(int d){
data = d;
left = NULL;
right = NULL;
}
};
void bottomView(node *root, map<int,int> &m, int l){
// base case
if(!root){
return;
}
// rec case
m[l] = root->data;
bottomView(root->left,m,l-1);
bottomView(root->right,m,l+1);
}
// Level Order Build of BT
node *buildTree(vector<int> v, int i){
if(v[i] == -1){
return NULL;
}
node *root = new node(v[i]);
root->left = buildTree(v,2*i);
root->right = buildTree(v,2*i+1);
return root;
}
int main() {
vector<int> v;
v.push_back(-1);
while(cin){
int x;
cin>>x;
v.push_back(x);
}
node *root = buildTree(v,1);
map<int,int> m;
int line = 0;
bottomView(root,m,line);
for(auto i:m){
cout<<i.second<<" ";
}
}