Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 14x 8x 6x 6x 6x 6x 6x | import { TreeNode } from './Tree'
export default function buildTree(
inorder: number[],
postorder: number[],
): TreeNode<number> | null {
if (postorder.length === 0)
return null
const root = new TreeNode(postorder[postorder.length - 1])
const postorderIndex = inorder.indexOf(postorder[postorder.length - 1])
root.left = buildTree(
inorder.slice(0, postorderIndex),
postorder.slice(0, postorderIndex),
)
root.right = buildTree(
inorder.slice(postorderIndex + 1),
postorder.slice(postorderIndex, postorder.length - 1),
)
return root
}
|