Given two binary search trees with identical structure, same node value but just one node with different values, how can we determine this node (by determining the node I mean the path from root to this node).
An obvious solution is to do traversal for both the trees and keep track of the path. But this would take O(n).
My question is can we do better than O(n) for this problem?
We can do an algorithm with O(n) pre-calculation and O(log(n)) query: in each node, in addition to the value of the node, we also hold the sum of the subtree of the node. Then, in order to find the different node, we do a search like this:
This is O(depth of result node), which is equivalent with O(log(n))
Shouldn't
O(depth of result node)
be upper bounded byO(n)
. Think of a path like BST, values inserted in increasing order, and last nodes mismatch.I had initially assumed that the question refers to balanced binary search trees (in which case, "depth of result node" <= log(n)). If we're talking about unbalanced binary search trees, then the problem's worst case seems to be O(n), as you said.
If we aren't allowed to hold any additional data in each node, I think it is impossible in the worst case. If, for instance, we are at a node, and both children nodes have the same value, there is no way to decide in which child the result is (once again, if we aren't allowed to hold any additional data). In this case, the result node might be in the last node in which we look, which would lead to an O(n) worst case complexity.
Can someone give me some resources to learn binary trees and binary search trees? Also practice problems for it. I shall be thankful to him.