Relative Content

Tag Archive for c++time-complexitybinary-treespace-complexitydsa

Duplicate Subtree in a Binary tree Time and space complexity

I saw the following question on GFG to find if there’s a duplicate subtree of size 2 or more in a binary tree. Now, it is written in the practice question requirements and also articles everywhere say that TC and Space complexity are both O(N) here, but I think it’s O(N^2) since subtrees here is a unordered_set of size N in worst case and each element in the worst case can have size N (not all will have size N, but even then space comes to be O(N^2)). Also, since string concatenation is O(N), the time complexity also seems like O(N^2). Am I understanding something wrong here?