Time: O(log(n)), Space:O(log(n))