Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In a tree data structure, which node is called the root?
easy
A. The topmost node with no parent
B. A node with no children
C. Any node at the bottom level
D. A node with exactly two children

Solution

  1. Step 1: Understand the root node definition

    The root node is the starting point of the tree and has no parent node above it.
  2. Step 2: Compare options with definition

    The topmost node with no parent matches this definition exactly; other options describe leaf or internal nodes.
  3. Final Answer:

    The topmost node with no parent -> Option A
  4. Quick Check:

    Root = topmost node with no parent [OK]
Hint: Root is always the very first node, no parent above it [OK]
Common Mistakes:
  • Confusing root with leaf nodes
  • Thinking root must have children
  • Assuming root is at the bottom
2. Which of the following is the correct way to describe a leaf node in a tree?
easy
A. The root node
B. A node with at least one child
C. A node with no children
D. A node with exactly one child

Solution

  1. Step 1: Define leaf node

    A leaf node is a node that does not have any children; it is at the end of a branch.
  2. Step 2: Match options to definition

    Only A node with no children states the node has no children, which matches the leaf node definition.
  3. Final Answer:

    A node with no children -> Option C
  4. Quick Check:

    Leaf = node with no children [OK]
Hint: Leaf nodes have zero children, always at branch ends [OK]
Common Mistakes:
  • Thinking leaf nodes have children
  • Confusing leaf with root
  • Assuming leaf nodes must have one child
3. Consider the following tree structure:
        A
       / \
      B   C
     /   / \
    D   E   F

What is the height of node B?
medium
A. 1
B. 2
C. 3
D. 0

Solution

  1. Step 1: Understand height of a node

    Height is the number of edges on the longest path from the node down to a leaf.
  2. Step 2: Calculate height of node B

    Node B has one child D which is a leaf. The longest path from B to leaf is B->D with 1 edge.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Height of B = 1 edge to leaf D [OK]
Hint: Height counts edges down to farthest leaf [OK]
Common Mistakes:
  • Counting nodes instead of edges
  • Confusing height with depth
  • Counting path to root instead of leaf
4. Given this tree:
        X
       / \
      Y   Z
     / 
    W  

What is the depth of node W?
Note: Depth is the number of edges from the root to the node.
medium
A. 1
B. 2
C. 3
D. 0

Solution

  1. Step 1: Understand depth definition

    Depth is the number of edges from the root node to the target node.
  2. Step 2: Count edges from root X to W

    Path is X -> Y -> W, which has 2 edges.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Depth of W = 2 edges from root [OK]
Hint: Depth counts edges up from root to node [OK]
Common Mistakes:
  • Counting nodes instead of edges
  • Confusing depth with height
  • Starting count from 1 instead of 0 edges
5. In a tree, the level of a node is defined as its depth plus one.
Given the tree:
        M
       / \
      N   O
         / \
        P   Q

What is the level of node P?
hard
A. 2
B. 1
C. 4
D. 3

Solution

  1. Step 1: Calculate depth of node P

    Path from root M to P is M -> O -> P, which has 2 edges, so depth = 2.
  2. Step 2: Calculate level using formula

    Level = depth + 1 = 2 + 1 = 3.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Level = depth + 1 = 3 [OK]
Hint: Level = depth + 1, count edges then add one [OK]
Common Mistakes:
  • Using depth as level without adding 1
  • Counting nodes incorrectly
  • Confusing level with height
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard