0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the inorder traversal output of this binary tree?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number, left: Node | null = null, right: Node | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(2, new Node(1), new Node(3));

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

console.log(inorderTraversal(root));
A[1, 2, 3]
B[1, 3, 2]
C[3, 2, 1]
D[2, 1, 3]
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left child first, then root, then right child.
Predict Output
intermediate
2:00remaining
What is the inorder traversal output of this unbalanced tree?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number, left: Node | null = null, right: Node | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

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

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

console.log(inorderTraversal(root));
A[1, 2, 3, 4, 5]
B[5, 4, 3, 2, 1]
C[4, 2, 1, 3, 5]
D[2, 1, 3, 4, 5]
Attempts:
2 left
💡 Hint
Traverse left subtree fully before root, then right subtree.
🔧 Debug
advanced
2:00remaining
Why does this inorder traversal code cause a runtime error?
Find the cause of the runtime error in this inorder traversal function.
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number, left: Node | null = null, right: Node | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function inorderTraversal(root: Node | null): number[] {
  if (root === null) return [];
  const left = inorderTraversal(root.left);
  const right = inorderTraversal(root.right);
  return left.concat([root.val]).concat(right);
}

const root = new Node(1);
console.log(inorderTraversal(root));
ANo error, outputs [1]
BSyntaxError due to missing semicolon
CStack overflow due to infinite recursion
DTypeError because concat expects an array but root.val is a number
Attempts:
2 left
💡 Hint
Check the types passed to concat method.
Predict Output
advanced
2:00remaining
What is the inorder traversal output of this tree with null children?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number, left: Node | null = null, right: Node | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(10, null, new Node(20, new Node(15), null));

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

console.log(inorderTraversal(root));
A[20, 15, 10]
B[10, 15, 20]
C[15, 20, 10]
D[10, 20, 15]
Attempts:
2 left
💡 Hint
Remember to visit left subtree before root, even if left is null.
🧠 Conceptual
expert
2:00remaining
How many nodes are visited before the root in inorder traversal of a complete binary tree of height 3?
Consider a complete binary tree of height 3 (levels 0 to 3). How many nodes are visited before the root node during an inorder traversal?
A4
B6
C7
D3
Attempts:
2 left
💡 Hint
Count all nodes in the left subtree of the root.