Here’s the link to my solution https://ide.codingblocks.com/s/63481
@sanjeetboora
@Khushboo
Tree bottom view - unable to pass test case 2
Hi Lakshay, consider the following test case:
Input:
20 8 22 5 3 4 25 -1 -1 10 14 -1 -1 -1 -1 -1 -1 -1 -1
Expected Output:
5 10 4 14 25
Your code’s output:
0 10 4 14 25
@Khushboo Can you tell me how the input is processed?
tree* buildLevelOrder(long long int *a,long long int i, long long int n){ if(a[i]==-1){ return NULL; } tree* root = new tree(a[i]); if(2*i +1 < n){ root->left = buildLevelOrder(a,2*i+1,n); } if(2*i+2<n){ root->right = buildLevelOrder(a,2*i+2,n); } return root; }
It was not explained in any of the course video.
and also this:
while(scanf("%d",&x)!=EOF){ a[i] = x; i++; }
Also can you tell me where am i wrong? why am i failing for bottom view https://ide.codingblocks.com/s/63481 WHY does I get an extra 0 comes as leaf node?
I have changed the code but it is a JUGAAD. Can you tell me better way of taking input when -1 is used for nulls in input
lakshay make a queue and push the root node into it and a null
now just apply while queue is not empty
extract the value from root and ask for its left and right child
and when null comes in queue pop null this means the one level is over now and then again push the null
In this way take the input of the tree
Hey Lakshay, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.