Segmentation Fault
Please look into my code as for all test cases I am getting TLE
I made required changes and code can be seen here: https://ide.codingblocks.com/s/208852
-
the TLE was due to problem in insertBST() code.
-
main changes are in modifyBST() method, please have a look into it. and try to dry run modifyBST() code through a sample example, then you will be able to understand the code.
-
the problem statement is quite misleading. according to sample example, it looks like they are talking about balanced BST. so you need to form balanced BST instead of simple BST, then run modifyBST() code.
you can see https://hack.codingblocks.com/app/contests/1483/125/problem here , that they are forming balanced BST. so you need to modify your code a bit more for balanced BST.
thanks
modify_BST function is not returning any value to the main function though it is declared as an integer data type function. Also the code is not executing. Please look into it.
Thanks for your help.
the return value of modifyBST is only needed in the function itself. it has no use in main method.
the code has no issues, I checked it already in few test cases before sending it to you.
although the hackerblocks platform has some issues, it sometimes don’t give any response. try more times.
this code won’t get submitted in hackerblocks as I already told you that hackerblock expects balance BST. please modify code to form a balanced BST.
thanks
The code is not expecting a balanced BST. Please look into this explanation: -
Sample Input
7
20 30 40 50 60 70 80
Sample Output
260 330 350 300 150 210 80
Explanation
The original tree looks like
50
/ \
30 70
/ \ / \
20 40 60 80
We are supposed to replace the elements by the sum of elements larger than it.
80 being the largest element remains unaffected .
70 being the second largest element gets updated to 150 (70+80)
60 becomes 210 (60 + 70 + 80)
50 becomes 260 (50 + 60 + 70 + 80)
40 becomes 300 (40 + 50 + 60 + 70 + 80)
30 becomes 330 (30 + 40 + 50 + 60 + 70 + 80)
20 becomes 350 (20 + 30 + 40 + 50 + 60 + 70 + 80)
The new tree looks like
260
/ \
330 150
/ \ / \
350 300 210 80
The Pre-Order traversal (Root->Left->Right) looks like :
260 330 350 300 150 210 80.
what should be binary search tree for above input? you created the bst for this input. check its preorder traversal and try to match it with the preorder traversal given in the explanation.
acc to me the bst for above input should be a right skewed tree.
thanks
I built the tree using build from array function and the code passed all the testcases with the same code. The input format given in the question is a bit confusing.
Anyway, thanks for your help.
thats great… please resolve and rate it.
thanks
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.