Binary tree insertion doubt

cant understand the significance of writing root=nn in line 44 of my code bcoz without that am not able to print the tree
https://ide.codingblocks.com/s/45378

for this input
10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false false

Hey that’s because you need to update your root node before return it…

but am updating root->left and root->right and its all that matters cause the main root will anyways be returned to main function so why
am calling nn->left and nn->right for insertion so and returning the pointers of newly formed nodes so why update root

Okay that’s fine if you want to return nn, you can do so but then you have to update line : 26 and line : 27 as shown below

root->left=insert(root->left);
root->right=insert(root->right);

this is because you have to update root’s left and right with the node return from the recursive function call.

sorry i dont get the reason why my way isnt working
returning nn is fine we both agree
but then
when am inserting somthing in right and left of nn which means new node then am gonna call nn->left=insert(nn->left) and same with right so why not this
i dont get the point where something goes wrong
am not traversing just building so why use root->left=insert(root->left);

okay, tell me one thing you are saying that you are doing this nn->left=insert(nn->left) that is you are initializing ‘nn->left’ with the node returned by 'insert(nn->left)` then why are you not doing this for root’s left and right.

simple bcoz :smile: i think thats not required

As its required for nn similarly it is required for root also.

i will think over this a little bit and then will ask if i have any more doubts in this

Yup Sure :slight_smile:

found my mistake
u were right line 26 and 27 need to be changed

Great :+1:
Keep coding :slight_smile: