Help with error

prob: https://hack.codingblocks.com/app/contests/2022/1884/problem

sol: https://ide.codingblocks.com/s/417242

the solution is not complete but I am getting a seg fault on what I have done so far

Your Mistake
if(root->data > data){
root->left = build(data, root);
}
else{
root->right = build(data, root);
}

correct statement

if(root->data > data){
root->left = build(data, root->left);
}
else{
root->right = build(data, root->right);
}


please check now

your logic is not correct modify the logic

code runs fine (no error)

okay I think I have to do k-- only once but I don’t understand why

Modified code

first call for right subtree
because the right most node in bst is largest node