0
0
DSA Typescriptprogramming~10 mins

Tree Traversal Inorder Left Root Right 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 visit the left subtree first in inorder traversal.

DSA Typescript
function inorder(node: TreeNode | null): void {
  if (node === null) return;
  inorder(node[1]);
  console.log(node.val);
  inorder(node.right);
}
Drag options to blanks, or click blank then click option'
A.left
B.val
C.parent
D.right
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left for the first recursive call.
Trying to access node.val instead of a child node.
2fill in blank
medium

Complete the code to print the root node value in inorder traversal.

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

Fix the error in the inorder traversal recursive call for the right subtree.

DSA Typescript
function inorder(node: TreeNode | null): void {
  if (node === null) return;
  inorder(node.left);
  console.log(node.val);
  inorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.parent
B.left
C.right
D.val
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorder(node.left) again instead of node.right.
Trying to call inorder(node.val) which is a value, not a node.
4fill in blank
hard

Fill both blanks to complete the inorder traversal that collects values in an array.

DSA Typescript
function inorder(node: TreeNode | null, result: number[]): void {
  if (node === null) return;
  inorder(node[1], result);
  result.push(node[2]);
  inorder(node.right, result);
}
Drag options to blanks, or click blank then click option'
A.left
B.val
C.right
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right in the first recursive call instead of node.left.
Pushing node.right or node.left instead of node.val.
5fill in blank
hard

Fill all three blanks to complete inorder traversal that returns an array of values.

DSA Typescript
function inorder(node: TreeNode | null): number[] {
  if (node === null) return [];
  const left = inorder(node[1]);
  const right = inorder(node[2]);
  return [...left, node[3], ...right];
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.val
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right children in recursive calls.
Using node.parent or node.val in recursive calls instead of child nodes.
Returning node instead of node.val in the combined array.