Dfs Algorithm Python Binary Search