0
0
DSA Typescriptprogramming~20 mins

Tree Terminology Root Leaf Height Depth Level in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output of Tree Node Levels
What is the printed level of each node after running this code?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  level: number;
  constructor(value: number) {
    this.value = value;
    this.children = [];
    this.level = 0;
  }
}

function assignLevels(root: TreeNode, currentLevel: number = 0): void {
  root.level = currentLevel;
  for (const child of root.children) {
    assignLevels(child, currentLevel + 1);
  }
}

const root = new TreeNode(1);
const child1 = new TreeNode(2);
const child2 = new TreeNode(3);
const grandchild = new TreeNode(4);
root.children.push(child1, child2);
child1.children.push(grandchild);

assignLevels(root);

console.log(root.level, child1.level, child2.level, grandchild.level);
A0 0 0 0
B0 1 1 2
C1 2 2 3
D1 1 1 1
Attempts:
2 left
šŸ’” Hint
Think about how levels increase as you go down from the root.
🧠 Conceptual
intermediate
1:30remaining
Identify Leaf Nodes in a Tree
Which nodes are leaf nodes in the following tree structure? Root (1) ā”œā”€ Child A (2) │ └─ Grandchild A1 (4) └─ Child B (3) ā”œā”€ Grandchild B1 (5) └─ Grandchild B2 (6)
ANodes 4, 5, 6
BNodes 1, 2, 3
CNodes 2, 3, 4
DNodes 1, 5, 6
Attempts:
2 left
šŸ’” Hint
Leaf nodes have no children.
ā“ Predict Output
advanced
2:00remaining
Calculate Height of a Tree
What is the output of the height calculated by this code for the given tree?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  constructor(value: number) {
    this.value = value;
    this.children = [];
  }
}

function height(node: TreeNode | null): number {
  if (!node) return -1;
  if (node.children.length === 0) return 0;
  let maxChildHeight = -1;
  for (const child of node.children) {
    const childHeight = height(child);
    if (childHeight > maxChildHeight) maxChildHeight = childHeight;
  }
  return maxChildHeight + 1;
}

const root = new TreeNode(1);
const c1 = new TreeNode(2);
const c2 = new TreeNode(3);
const gc1 = new TreeNode(4);
const gc2 = new TreeNode(5);
root.children.push(c1, c2);
c1.children.push(gc1);
c2.children.push(gc2);

console.log(height(root));
A2
B0
C1
D3
Attempts:
2 left
šŸ’” Hint
Height is the longest path from root to a leaf in edges.
šŸ”§ Debug
advanced
2:00remaining
Find the Depth of a Node
What is the depth of node with value 5 after running this code?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  constructor(value: number) {
    this.value = value;
    this.children = [];
  }
}

function findDepth(root: TreeNode, target: number, currentDepth: number = 0): number {
  if (root.value === target) return currentDepth;
  for (const child of root.children) {
    const d = findDepth(child, target, currentDepth + 1);
    if (d !== -1) return d;
  }
  return -1;
}

const root = new TreeNode(1);
const c1 = new TreeNode(2);
const c2 = new TreeNode(3);
const gc1 = new TreeNode(4);
const gc2 = new TreeNode(5);
root.children.push(c1, c2);
c1.children.push(gc1);
c2.children.push(gc2);

console.log(findDepth(root, 5));
A-1
B3
C2
D1
Attempts:
2 left
šŸ’” Hint
Depth counts edges from root to the node.
🧠 Conceptual
expert
1:30remaining
Level vs Depth vs Height
Which statement correctly describes the difference between level, depth, and height of a node in a tree?
ADepth is the number of children; level is the distance from root to leaf; height is the distance from leaf to root.
BHeight and depth are the same; level is the number of children a node has.
CLevel is the number of children; depth is the number of leaves; height is the number of nodes in the tree.
DLevel and depth are the same; height is the number of edges on the longest path from the node to a leaf.
Attempts:
2 left
šŸ’” Hint
Think about how far a node is from the root and from the leaves.