What about Time & Space complexity and which solution (iteratively && recursive) could be better?
@codemonkey26143 жыл бұрын
I guess recursive time complexity should be Big O of N (the total number of nodes) under the worse case in which L equals the smallest node and R equals the biggest node in BST.
@Randomguu3 жыл бұрын
couple of points recursive is always worse, the java virtual machine needs to maintain the recursive stack pointers so thats more space and they are located randomly within your ram so it cannot be easily cached by cpu so time is also worse, also we never take advantage of the binary nature of the tree, I would go for binary search to find first element bigger than L and then do in order traversal on its right subtree until wwe reach > R then stop. Think thats the most optimal approach