Complete the code to start the postorder traversal by visiting the left child first.
function postorder(node: TreeNode | null): void {
if (node === null) return;
postorder(node.[1]);
// continue traversal
}In postorder traversal, we visit the left child first before right and root.
Complete the code to visit the right child after the left child in postorder traversal.
function postorder(node: TreeNode | null): void {
if (node === null) return;
postorder(node.left);
postorder(node.[1]);
// continue traversal
}After visiting the left child, postorder traversal visits the right child.
Fix the error in the code to correctly print the root node's value after visiting children.
function postorder(node: TreeNode | null): void {
if (node === null) return;
postorder(node.left);
postorder(node.right);
console.log(node.[1]);
}We print the node's value after visiting left and right children in postorder.
Fill both blanks to create a postorder traversal that collects node values in an array.
function postorderCollect(node: TreeNode | null, result: number[]): void {
if (node === null) return;
postorderCollect(node.[1], result);
postorderCollect(node.[2], result);
result.push(node.value);
}Postorder visits left child first, then right child, then root.
Fill all three blanks to create a postorder traversal that returns an array of node values.
function postorderReturn(node: TreeNode | null): number[] {
if (node === null) return [];
const leftVals = postorderReturn(node.[1]);
const rightVals = postorderReturn(node.[2]);
return [...leftVals, ...rightVals, node.[3]];
}Postorder traversal collects left subtree values, then right subtree values, then the current node's value.