0
0
DSA Typescriptprogramming~20 mins

Tree Traversal Postorder Left Right Root in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of postorder traversal on this tree?

Given the binary tree below, what is the postorder traversal output?

Tree structure:

    1
   / \
  2   3
 / \
4   5
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function postorderTraversal(root: TreeNode | null): number[] {
  if (!root) return [];
  return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val];
}

const root = new TreeNode(1,
  new TreeNode(2, new TreeNode(4), new TreeNode(5)),
  new TreeNode(3)
);

console.log(postorderTraversal(root));
A[4, 5, 2, 3, 1]
B[1, 2, 3, 4, 5]
C[2, 4, 5, 3, 1]
D[4, 2, 5, 3, 1]
Attempts:
2 left
💡 Hint

Remember postorder visits left subtree, then right subtree, then root.

Predict Output
intermediate
2:00remaining
What is the postorder traversal output of this tree?

Consider this binary tree:

      10
     /  \
    5    15
        /  \
       12   20

What is the postorder traversal output?

DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function postorderTraversal(root: TreeNode | null): number[] {
  if (!root) return [];
  return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val];
}

const root = new TreeNode(10,
  new TreeNode(5),
  new TreeNode(15, new TreeNode(12), new TreeNode(20))
);

console.log(postorderTraversal(root));
A[5, 15, 12, 20, 10]
B[12, 20, 15, 5, 10]
C[10, 5, 15, 12, 20]
D[5, 12, 20, 15, 10]
Attempts:
2 left
💡 Hint

Postorder visits left subtree, then right subtree, then root.

🔧 Debug
advanced
2:00remaining
Why does this postorder traversal code cause a runtime error?

Look at this TypeScript code for postorder traversal. It throws an error when run. What is the cause?

DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function postorderTraversal(root: TreeNode | null): number[] {
  if (root === null) return [];
  const left = postorderTraversal(root.left!);
  const right = postorderTraversal(root.right!);
  return [...left, ...right, root.val];
}

const root = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(postorderTraversal(root));
AUsing '!' operator on possibly null left or right causes runtime error when child is null.
BThe TreeNode constructor is missing initialization of left and right properties.
CThe function returns in wrong order: root, left, right instead of left, right, root.
DMissing base case for null root causes infinite recursion.
Attempts:
2 left
💡 Hint

Check how the code handles null children with the '!' operator.

🧠 Conceptual
advanced
1:30remaining
Which traversal order matches postorder traversal?

Which of these sequences correctly describes the order of visiting nodes in postorder traversal?

AVisit root node, then right subtree, then left subtree.
BVisit left subtree, then right subtree, then root node.
CVisit root node, then left subtree, then right subtree.
DVisit right subtree, then left subtree, then root node.
Attempts:
2 left
💡 Hint

Postorder means the root is visited last.

Predict Output
expert
3:00remaining
What is the postorder traversal output of this complex tree?

Given the following binary tree, what is the postorder traversal output?

          7
         / \
        3   9
       /   / \
      1   8  10
       \      /
        2    5
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function postorderTraversal(root: TreeNode | null): number[] {
  if (!root) return [];
  return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val];
}

const root = new TreeNode(7,
  new TreeNode(3, new TreeNode(1, null, new TreeNode(2)), null),
  new TreeNode(9, new TreeNode(8), new TreeNode(10, new TreeNode(5), null))
);

console.log(postorderTraversal(root));
A[2, 1, 3, 8, 10, 5, 9, 7]
B[1, 2, 3, 8, 5, 10, 9, 7]
C[2, 1, 3, 8, 5, 10, 9, 7]
D[2, 1, 3, 8, 10, 9, 5, 7]
Attempts:
2 left
💡 Hint

Carefully follow left, right, root order recursively.