0
0
DSA Typescriptprogramming~20 mins

Create a Binary Tree Manually in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Mastery
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 manual binary tree traversal?

Consider the following TypeScript code that creates a simple binary tree manually and performs an inorder traversal (left, root, right). What will be the printed output?

DSA Typescript
class TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;

  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(7);

function inorder(node: TreeNode | null): void {
  if (!node) return;
  inorder(node.left);
  console.log(node.value);
  inorder(node.right);
}

inorder(root);
A10\n5\n15\n3\n7
B5\n3\n7\n10\n15
C3\n5\n7\n10\n15
D3\n7\n5\n10\n15
Attempts:
2 left
💡 Hint

Remember inorder traversal visits left child, then root, then right child.

🧠 Conceptual
intermediate
1:30remaining
How many nodes are in this manually created binary tree?

Given the following manual binary tree creation code, how many nodes does the tree contain?

DSA Typescript
class TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;

  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(6);
A4
B6
C7
D5
Attempts:
2 left
💡 Hint

Count each new TreeNode created and assigned.

🔧 Debug
advanced
2:00remaining
What error occurs when running this manual binary tree code?

Examine the following TypeScript code that attempts to create a binary tree. What error will it produce when run?

DSA Typescript
class TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;

  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left.left = new TreeNode(6);
ATypeError: Cannot set property 'left' of null
BSyntaxError: Unexpected token '.'
CNo error, tree created successfully
DReferenceError: root.right.left is not defined
Attempts:
2 left
💡 Hint

Check if all intermediate nodes exist before assigning children.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a manual binary tree node with left and right children?

Choose the correct TypeScript code snippet that creates a binary tree node with value 10 and assigns left child 5 and right child 15.

A
const node = new TreeNode(10);
node.left = new TreeNode(5);
node.right = new TreeNode(15);
B
const node = TreeNode(10);
node.left = TreeNode(5);
node.right = TreeNode(15);
C
const node = new TreeNode(10)
node.left = new TreeNode(5)
node.right = new TreeNode(15)
D
const node = new TreeNode(10);
node.left = 5;
node.right = 15;
Attempts:
2 left
💡 Hint

Remember to use new keyword and assign TreeNode objects to left and right.

🚀 Application
expert
2:30remaining
What is the preorder traversal output of this manually created binary tree?

Given the following manual binary tree creation, what is the output of a preorder traversal (root, left, right)?

DSA Typescript
class TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;

  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(8);
root.left = new TreeNode(3);
root.right = new TreeNode(10);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(6);
root.left.right.left = new TreeNode(4);
root.left.right.right = new TreeNode(7);
root.right.right = new TreeNode(14);
root.right.right.left = new TreeNode(13);

function preorder(node: TreeNode | null): void {
  if (!node) return;
  console.log(node.value);
  preorder(node.left);
  preorder(node.right);
}

preorder(root);
A1\n3\n4\n6\n7\n8\n10\n13\n14
B3\n1\n6\n4\n7\n8\n10\n14\n13
C8\n10\n14\n13\n3\n6\n4\n7\n1
D8\n3\n1\n6\n4\n7\n10\n14\n13
Attempts:
2 left
💡 Hint

Preorder visits root first, then left subtree, then right subtree.