You are given two separate coding tasks.
You have n courses labeled 0..n-1 and a list of prerequisite pairs prerequisites, where each pair [a, b] means to take course a, you must first take course b.
Task: Return any valid ordering of all courses that satisfies prerequisites. If it is impossible (because of a cycle), return an empty list.
Input:
n
(integer)
prerequisites
(list of pairs)
Output:
n
representing a valid order, or
[]
if no order exists.
Constraints (typical):
1 ≤ n ≤ 10^5
0 ≤ len(prerequisites) ≤ 2*10^5
Notes:
You are given the root of a binary tree. The tree is guaranteed to be complete and balanced (interview variant constraint), but your solution may work for any binary tree.
Define the boundary of the tree in anti-clockwise order as:
root.left
going downward, always taking the next boundary node.
root.right
going downward, collected top-down but output
bottom-up
.
Task: Return a list of node values in boundary order, with no duplicates.
Input:
root
of a binary tree
Output:
Edge cases to consider:
Complexity target:
O(N)
, space
O(H)
(recursion) or
O(N)
worst case depending on implementation.