0
0
DSA Typescriptprogramming~20 mins

Binary Tree Node Structure in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Tree Node Creation
What will be the printed output of the binary tree node after running the code below?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
console.log(root.val + ' -> ' + root.left?.val + ' -> ' + root.right?.val + ' -> null');
A10 -> 5 -> 15 -> null
B10 -> null -> null -> null
C15 -> 5 -> 10 -> null
D5 -> 10 -> 15 -> null
Attempts:
2 left
💡 Hint
Think about how the root node and its children are assigned values.
Predict Output
intermediate
2:00remaining
Value of Left Child After Node Modification
After running the code below, what is the value of root.left.val?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.left.val = 15;
A15
B10
C20
Dnull
Attempts:
2 left
💡 Hint
Look at the line where root.left.val is changed.
Predict Output
advanced
3:00remaining
Output of Inorder Traversal of Binary Tree
What is the output of the inorder traversal of the binary tree created by the code below?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function inorder(root: TreeNode | null, result: number[] = []): number[] {
  if (root === null) return result;
  inorder(root.left, result);
  result.push(root.val);
  inorder(root.right, result);
  return result;
}

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.right.right = new TreeNode(14);
const output = inorder(root);
console.log(output.join(' -> ') + ' -> null');
A8 -> 3 -> 1 -> 6 -> 10 -> 14 -> null
B1 -> 3 -> 6 -> 8 -> 10 -> 14 -> null
C1 -> 6 -> 3 -> 8 -> 14 -> 10 -> null
D3 -> 1 -> 6 -> 8 -> 14 -> 10 -> null
Attempts:
2 left
💡 Hint
Inorder traversal visits left child, then node, then right child.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Tree Node Assignment
What error will the following code produce when run?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(5);
root.left.val = 3;
ANo error, runs successfully
BSyntaxError: Unexpected token '.'
CReferenceError: root.left is not defined
DTypeError: Cannot read property 'val' of null
Attempts:
2 left
💡 Hint
Check if root.left is assigned before accessing its val property.
🧠 Conceptual
expert
3:00remaining
Number of Nodes in a Complete Binary Tree of Height h
What is the total number of nodes in a complete binary tree of height h (where height is the number of edges from root to deepest leaf)?
Ah + 1
B2^h - 1
C2^(h+1) - 1
Dh * 2
Attempts:
2 left
💡 Hint
Think about how nodes double at each level in a complete binary tree.