Question on time complexity

convert sorted linked list to bst (leet code)

in the last solution of inorder simulation it stil follows the recursion tree even if it visits a node in linked list once , so shouldnt the time complextity be somethin else other than linear

Hey, it totally depends on your implementation if you are using the method in which you are everytime find out the middle element and then consider it as a mid and after that checking for the left and right subtree. All this approach takes O(nlogn) time.
And the approach in which we read every element only once and proceed further it is like we are creating tree from leaves to root. This approach takes O(n) time.
If you are using the second approach then yes the complexity will be linear.
And if we say we are reading the element twice or thrice we still consider it in O(n) time complexity, as we need to see the variable which reaches the higher in very fast. I meant to say nlogn as always an upper bound on O(n) but if we take nlogC then O(n) then here logC will be considered as a constant and we still consider it to be O(n).
I hope it is clear to you, still in case there is some confusion or query, pls let me know. I will try to help you out.
And incase it is clear to you pls provide the rating as well as feedback. It is an important process so that we can grow ourselves.
Thanks :slight_smile:
Happy Coding !!

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.