Challenge - 5 Problems
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tree Node Levels
What is the output of the following C# code that prints the level of each node in a simple tree?
DSA C#
using System; using System.Collections.Generic; class TreeNode { public int Value; public List<TreeNode> Children = new List<TreeNode>(); public TreeNode(int val) { Value = val; } } class Program { static void PrintLevels(TreeNode node, int level) { Console.WriteLine($"Node {node.Value} is at level {level}"); foreach (var child in node.Children) { PrintLevels(child, level + 1); } } static void Main() { TreeNode root = new TreeNode(1); TreeNode child1 = new TreeNode(2); TreeNode child2 = new TreeNode(3); TreeNode grandchild = new TreeNode(4); root.Children.Add(child1); root.Children.Add(child2); child1.Children.Add(grandchild); PrintLevels(root, 0); } }
Attempts:
2 left
💡 Hint
Remember that the root node starts at level 0 and each child node increases the level by 1.
✗ Incorrect
The root node is at level 0. Its children are at level 1, and their children are at level 2. The code recursively prints each node's level correctly.
🧠 Conceptual
intermediateIdentify Leaf Nodes in a Tree
In a tree, which nodes are called leaf nodes?
Attempts:
2 left
💡 Hint
Leaf nodes do not have any nodes connected below them.
✗ Incorrect
Leaf nodes are nodes that have no children. They are the end points of the tree branches.
❓ Predict Output
advancedCalculate Height of a Tree
What is the output of this C# code that calculates the height of a tree?
DSA C#
using System; using System.Collections.Generic; class TreeNode { public int Value; public List<TreeNode> Children = new List<TreeNode>(); public TreeNode(int val) { Value = val; } } class Program { static int Height(TreeNode node) { if (node.Children.Count == 0) return 0; int maxChildHeight = 0; foreach (var child in node.Children) { int childHeight = Height(child); if (childHeight > maxChildHeight) maxChildHeight = childHeight; } return maxChildHeight + 1; } static void Main() { TreeNode root = new TreeNode(1); TreeNode child1 = new TreeNode(2); TreeNode child2 = new TreeNode(3); TreeNode grandchild1 = new TreeNode(4); TreeNode grandchild2 = new TreeNode(5); root.Children.Add(child1); root.Children.Add(child2); child1.Children.Add(grandchild1); child1.Children.Add(grandchild2); Console.WriteLine(Height(root)); } }
Attempts:
2 left
💡 Hint
Height is the longest path from root to a leaf node in terms of edges.
✗ Incorrect
The longest path from root to leaf is 2 edges: root->child1->grandchild1 or grandchild2. So height is 2.
🧠 Conceptual
advancedDepth of a Node in a Tree
What does the depth of a node in a tree represent?
Attempts:
2 left
💡 Hint
Depth measures how far a node is from the root.
✗ Incorrect
Depth is the count of edges from the root node down to the node in question.
🚀 Application
expertDetermine Level of Nodes After Insertions
Given the following insertions in a tree, what is the level of node with value 7?
DSA C#
using System; using System.Collections.Generic; class TreeNode { public int Value; public List<TreeNode> Children = new List<TreeNode>(); public TreeNode(int val) { Value = val; } } class Program { static int FindLevel(TreeNode node, int val, int level) { if (node.Value == val) return level; foreach (var child in node.Children) { int found = FindLevel(child, val, level + 1); if (found != -1) return found; } return -1; } static void Main() { TreeNode root = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); TreeNode n7 = new TreeNode(7); root.Children.Add(n2); root.Children.Add(n3); n2.Children.Add(n4); n2.Children.Add(n5); n3.Children.Add(n6); n6.Children.Add(n7); Console.WriteLine(FindLevel(root, 7, 0)); } }
Attempts:
2 left
💡 Hint
Count edges from root to node 7.
✗ Incorrect
Path: root(1) level 0 -> n3 level 1 -> n6 level 2 -> n7 level 3. So level is 3.
