Time: O(H*logn), Space: O(logn)