You are viewing a single comment's thread. Return to all comments →
You actually dont need to create a class. Here's a way using just function:
sys.setrecursionlimit(1 << 30) def create_tree(indexes): tree_arr = [-1 for _ in range(1025)] tree_arr[1] = 1 _next = [] frontier = [1] indexes_pos = 0 while frontier: for v in frontier: if indexes_pos >= len(indexes): return tree_arr if v != -1: left_c, right_c = indexes[indexes_pos][0], indexes[indexes_pos][1] tree_arr[v] = [left_c, right_c] _next.append(left_c) _next.append(right_c) indexes_pos += 1 frontier = _next _next = [] return tree_arr def swapNodes(indexes, queries): tree = create_tree(indexes) height = 1025 controller = [0 for _ in range(height)] traverse_res = [] def traverse(lvl, idx): if idx >= len(tree) or idx == -1: return left = tree[idx][0] right = tree[idx][1] if controller[lvl] == 1: left = tree[idx][1] right = tree[idx][0] traverse(lvl+1, left) traverse_res.append(idx) traverse(lvl+1, right) return res = [] for q in queries: tmp = q while q < height-1: controller[q-1] = not controller[q-1] q += tmp traverse(0, 1) res.append(traverse_res) traverse_res = [] return res
Seems like cookies are disabled on this browser, please enable them to open this website
Swap Nodes [Algo]
You are viewing a single comment's thread. Return to all comments →
You actually dont need to create a class. Here's a way using just function: