0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postorder 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 the postorder traversal?
Given the binary tree below, what is the output of the postorder traversal (Left, Right, Root)?

Tree structure:
1
/ \
2 3
/ \
4 5
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function postorder(root) {
  if (!root) return [];
  return [...postorder(root.left), ...postorder(root.right), root.value];
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);

console.log(postorder(root));
A[1, 2, 3, 4, 5]
B[2, 4, 5, 3, 1]
C[4, 5, 2, 3, 1]
D[4, 2, 5, 3, 1]
Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
Predict Output
intermediate
2:00remaining
What is the postorder traversal output for this tree?
Consider this binary tree:

10
/ \
20 30
/ \
40 50

What is the postorder traversal output (Left, Right, Root)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function postorder(root) {
  if (!root) return [];
  return [...postorder(root.left), ...postorder(root.right), root.value];
}

const root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);

console.log(postorder(root));
A[20, 40, 50, 30, 10]
B[10, 20, 30, 40, 50]
C[40, 50, 30, 20, 10]
D[20, 30, 40, 50, 10]
Attempts:
2 left
💡 Hint
Postorder visits left subtree first, then right subtree, then root.
🧠 Conceptual
advanced
1:30remaining
Which statement about postorder traversal is true?
Choose the correct statement about postorder traversal of a binary tree.
APostorder traversal visits the root node before its children.
BPostorder traversal visits left subtree, then right subtree, then the root node.
CPostorder traversal visits nodes in the order: Right, Left, Root.
DPostorder traversal visits nodes in the order: Root, Left, Right.
Attempts:
2 left
💡 Hint
Think about when the root node is visited in postorder.
🔧 Debug
advanced
2:00remaining
Find the error in this postorder traversal code
The following code is intended to perform a postorder traversal of a binary tree and return an array of node values. What is the error?
DSA Javascript
function postorder(root) {
  if (!root) return [];
  return [root.value, ...postorder(root.left), ...postorder(root.right)];
}
AThere is no error; this is correct postorder traversal.
BThe function will cause a runtime error because of incorrect spread syntax.
CThe function will return an empty array for any input.
DThe root node is visited before its children, so this is preorder traversal, not postorder.
Attempts:
2 left
💡 Hint
Check the order in which root and children are visited.
🚀 Application
expert
1:30remaining
How many nodes are visited before the root in postorder traversal?
In a postorder traversal of a binary tree with 7 nodes, how many nodes are visited before the root node is visited?
A6
B7
C0
DDepends on the tree structure
Attempts:
2 left
💡 Hint
Postorder visits all children before the root.