Given two lists of distinct integers, inorder and postorder, that are the inorder and postorder traversals of the same binary tree, rebuild that tree and return its root.
Examples
8
/ \
3 6
\ \
10 4
/
11
Input: inorder = [3, 10, 8, 6, 11, 4], postorder = [10, 3, 11, 4, 6, 8]
Output: tree_to_list(...) == [8, 3, 6, None, 10, None, 4, None, None, 11]
Input: inorder = [2, 1], postorder = [2, 1]
Output: [1, 2]
Constraints
0 <= len(inorder) == len(postorder) <= 1000- All values are distinct and the two lists describe a valid tree
Goals
- Read the postorder sequence from the back to obtain roots
- Build the right subtree before the left when consuming postorder in reverse