Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the order of nodes visited in a postorder tree traversal?
easy
A. Left subtree, Right subtree, Root node
B. Root node, Left subtree, Right subtree
C. Root node, Right subtree, Left subtree
D. Right subtree, Left subtree, Root node

Solution

  1. Step 1: Understand postorder traversal definition

    Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
  2. Step 2: Match the traversal order to options

    Left subtree, Right subtree, Root node correctly lists Left subtree, Right subtree, Root node order.
  3. Final Answer:

    Left subtree, Right subtree, Root node -> Option A
  4. Quick Check:

    Postorder = Left, Right, Root [OK]
Hint: Remember: postorder ends with root after children [OK]
Common Mistakes:
  • Confusing postorder with preorder or inorder
  • Visiting root before children
  • Swapping left and right subtree order
2. Which of the following TypeScript function signatures correctly represents a postorder traversal of a binary tree node?
easy
A. function postorder(node: TreeNode | null): TreeNode[]
B. function postorder(node: TreeNode): number
C. function postorder(): TreeNode
D. function postorder(node: TreeNode | null): void

Solution

  1. Step 1: Identify correct parameter and return type for traversal

    Traversal functions usually take a node or null and return void if printing or processing inline.
  2. Step 2: Check options for correct signature

    function postorder(node: TreeNode | null): void takes a node or null and returns void, suitable for traversal printing or processing.
  3. Final Answer:

    function postorder(node: TreeNode | null): void -> Option D
  4. Quick Check:

    Traversal function signature = function postorder(node: TreeNode | null): void [OK]
Hint: Traversal usually takes node|null and returns void [OK]
Common Mistakes:
  • Omitting null in parameter type
  • Returning incorrect types like TreeNode or number
  • Missing parameter in function signature
3. Given the following binary tree:
    1
   / \
  2   3
 / \
4   5

What is the output of a postorder traversal?
medium
A. [4, 5, 2, 3, 1]
B. [1, 2, 3, 4, 5]
C. [2, 4, 5, 3, 1]
D. [4, 2, 5, 3, 1]

Solution

  1. Step 1: Traverse left subtree of root (1)

    Left subtree rooted at 2: postorder visits 4, then 5, then 2.
  2. Step 2: Traverse right subtree and root

    Right subtree is 3, visited next. Finally, root 1 is visited.
  3. Final Answer:

    [4, 5, 2, 3, 1] -> Option A
  4. Quick Check:

    Postorder output = [4, 5, 2, 3, 1] [OK]
Hint: Postorder prints children before parent [OK]
Common Mistakes:
  • Visiting root before children
  • Mixing left and right subtree order
  • Confusing with preorder or inorder output
4. Consider this TypeScript postorder traversal function:
function postorder(node: TreeNode | null): void {
  if (!node) return;
  postorder(node.right);
  postorder(node.left);
  console.log(node.val);
}

What is the error in this code?
medium
A. It prints node value before children
B. It does not check for null nodes
C. It visits right subtree before left, reversing postorder
D. It returns a value but should be void

Solution

  1. Step 1: Analyze traversal order in code

    The function calls postorder on right child first, then left child, then prints root.
  2. Step 2: Compare with correct postorder order

    Correct postorder is left, right, root. This code reverses left and right order.
  3. Final Answer:

    It visits right subtree before left, reversing postorder -> Option C
  4. Quick Check:

    Left before right in postorder [OK]
Hint: Postorder = left first, then right, then root [OK]
Common Mistakes:
  • Swapping left and right subtree calls
  • Printing before visiting children
  • Ignoring null checks
5. You want to collect all node values of a binary tree in postorder traversal into an array using TypeScript. Which code snippet correctly implements this?
hard
A. function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; result.push(node.val); postorder(node.left, result); postorder(node.right, result); }
B. function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; postorder(node.left, result); postorder(node.right, result); result.push(node.val); }
C. function postorder(node: TreeNode | null, result: number[]): number[] { if (!node) return result; result.push(node.val); postorder(node.left, result); postorder(node.right, result); return result; }
D. function postorder(node: TreeNode | null): number[] { if (!node) return []; return [node.val, ...postorder(node.left), ...postorder(node.right)]; }

Solution

  1. Step 1: Understand postorder traversal collection

    Postorder visits left, then right, then root. Values should be pushed after children.
  2. Step 2: Check each option for correct order and pushing

    function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; postorder(node.left, result); postorder(node.right, result); result.push(node.val); } visits left, right, then pushes node value, correctly collecting postorder.
  3. Final Answer:

    function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; postorder(node.left, result); postorder(node.right, result); result.push(node.val); } -> Option B
  4. Quick Check:

    Push after children = postorder [OK]
Hint: Push node value after left and right calls [OK]
Common Mistakes:
  • Pushing node value before children
  • Returning new arrays instead of using passed array
  • Swapping left and right subtree calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard