0
0
DSA Typescriptprogramming~10 mins

Tree Traversal Postorder Left Right Root in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the postorder traversal by visiting the left child first.

DSA Typescript
function postorder(node: TreeNode | null): void {
  if (node === null) return;
  postorder(node.[1]);
  // continue traversal
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' for the first recursive call.
Trying to access 'parent' which is not part of traversal here.
2fill in blank
medium

Complete the code to visit the right child after the left child in postorder traversal.

DSA Typescript
function postorder(node: TreeNode | null): void {
  if (node === null) return;
  postorder(node.left);
  postorder(node.[1]);
  // continue traversal
}
Drag options to blanks, or click blank then click option'
Aparent
Broot
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'root' instead of 'right'.
Skipping the right child call.
3fill in blank
hard

Fix the error in the code to correctly print the root node's value after visiting children.

DSA Typescript
function postorder(node: TreeNode | null): void {
  if (node === null) return;
  postorder(node.left);
  postorder(node.right);
  console.log(node.[1]);
}
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cright
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Printing node.left or node.right instead of node.value.
Trying to print node.parent which may not exist.
4fill in blank
hard

Fill both blanks to create a postorder traversal that collects node values in an array.

DSA Typescript
function postorderCollect(node: TreeNode | null, result: number[]): void {
  if (node === null) return;
  postorderCollect(node.[1], result);
  postorderCollect(node.[2], result);
  result.push(node.value);
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right children.
Using 'parent' or 'root' which are not children.
5fill in blank
hard

Fill all three blanks to create a postorder traversal that returns an array of node values.

DSA Typescript
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]];
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cvalue
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Returning node.left or node.right instead of node.value.
Swapping left and right children.