0
0
DSA Javascriptprogramming~20 mins

Tree Terminology Root Leaf Height Depth Level in DSA Javascript - 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 output of the following code that prints the level of each node in a simple tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.children = [];
  }
}

const root = new Node(1);
const child1 = new Node(2);
const child2 = new Node(3);
const grandchild = new Node(4);

root.children.push(child1, child2);
child1.children.push(grandchild);

function printLevels(node, level = 0) {
  console.log(`${node.value}: Level ${level}`);
  for (const child of node.children) {
    printLevels(child, level + 1);
  }
}

printLevels(root);
A
1: Level 0
2: Level 0
4: Level 1
3: Level 0
B
1: Level 0
2: Level 1
4: Level 2
3: Level 1
C
1: Level 1
2: Level 2
4: Level 3
3: Level 2
D
1: Level 1
2: Level 1
4: Level 2
3: Level 1
Attempts:
2 left
💡 Hint
Think about the root node level and how it increases for children.
🧠 Conceptual
intermediate
1:00remaining
Identify Leaf Nodes in a Tree
Which of the following best describes a leaf node in a tree?
AA node with no children
BA node with exactly one child
CThe root node of the tree
DA node with two or more children
Attempts:
2 left
💡 Hint
Leaf nodes do not have any branches going out.
Predict Output
advanced
2:00remaining
Calculate Height of a Tree
What is the output of the following code that calculates the height of a tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.children = [];
  }
}

const root = new Node(10);
const c1 = new Node(20);
const c2 = new Node(30);
const c3 = new Node(40);
const gc1 = new Node(50);
const gc2 = new Node(60);

root.children.push(c1, c2);
c1.children.push(c3);
c3.children.push(gc1, gc2);

function height(node) {
  if (node.children.length === 0) return 0;
  let maxChildHeight = 0;
  for (const child of node.children) {
    const childHeight = height(child);
    if (childHeight > maxChildHeight) maxChildHeight = childHeight;
  }
  return maxChildHeight + 1;
}

console.log(height(root));
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Height is the longest path from root to leaf in edges.
🔧 Debug
advanced
2:00remaining
Find the Error in Depth Calculation
The following code tries to calculate the depth of a node in a tree but produces incorrect results. What is the error?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.parent = null;
  }
}

const root = new Node(1);
const child = new Node(2);
child.parent = root;
const grandchild = new Node(3);
grandchild.parent = child;

function depth(node) {
  let d = 0;
  while (node.parent !== null) {
    d += 1;
  }
  return d;
}

console.log(depth(grandchild));
ASyntax error due to missing semicolon
BReturns 0 because parent is always null
CReturns 1 instead of 2 because of wrong increment
DInfinite loop because node is not updated inside the while loop
Attempts:
2 left
💡 Hint
Check if the loop changes the node variable to move up the tree.
🧠 Conceptual
expert
1:30remaining
Understanding Tree Levels and Depths
In a tree, which statement correctly relates the level and depth of a node?
ALevel and depth of a node are always equal
BLevel is depth plus one
CDepth is the number of edges from root to node; level is depth plus one
DLevel is the number of edges from root to node; depth is level plus one
Attempts:
2 left
💡 Hint
Think about how level and depth are counted starting from root.