0
0
DSA Typescriptprogramming~10 mins

Tree Traversal Preorder Root Left 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 start preorder traversal by visiting the root node first.

DSA Typescript
function preorder(node: TreeNode | null): void {
  if (node === null) return;
  console.log(node.val);
  preorder([1]);
}
Drag options to blanks, or click blank then click option'
Anode
Bnode.right
Cnode.left
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on right child first
Calling preorder on the node itself again causing infinite recursion
2fill in blank
medium

Complete the code to continue preorder traversal by visiting the right subtree after the left.

DSA Typescript
function preorder(node: TreeNode | null): void {
  if (node === null) return;
  console.log(node.val);
  preorder(node.left);
  preorder([1]);
}
Drag options to blanks, or click blank then click option'
Anode.right
Bnode.left
Cnode
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on left child twice
Calling preorder on the node itself again
3fill in blank
hard

Fix the error in the preorder traversal function to avoid infinite recursion.

DSA Typescript
function preorder(node: TreeNode | null): void {
  if (node === null) return;
  console.log(node.val);
  preorder(node.left);
  preorder([1]);
}
Drag options to blanks, or click blank then click option'
Anode
Bnode.right
Cnull
Dnode.left
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on node itself
Calling preorder on null unnecessarily
4fill in blank
hard

Fill both blanks to complete the preorder traversal function visiting root, left, then right.

DSA Typescript
function preorder(node: TreeNode | null): void {
  if (node === null) return;
  console.log(node.val);
  preorder([1]);
  preorder([2]);
}
Drag options to blanks, or click blank then click option'
Anode.left
Bnode.right
Cnode
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right calls
Calling preorder on the node itself
5fill in blank
hard

Fill all three blanks to create a preorder traversal that prints the node value in uppercase, then visits left and right children.

DSA Typescript
function preorder(node: TreeNode | null): void {
  if (node === null) return;
  console.log(node.val[1]);
  preorder([2]);
  preorder([3]);
}
Drag options to blanks, or click blank then click option'
A.toUpperCase()
Bnode.left
Cnode.right
D.toLowerCase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .toLowerCase() instead of .toUpperCase()
Swapping left and right children calls