Challenge - 5 Problems
Binary Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this manual binary tree creation?
Consider the following C# code that creates a binary tree manually. What will be printed when we traverse the tree in preorder (root, left, right)?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } public class BinaryTree { public Node Root; public void Preorder(Node node) { if (node == null) return; System.Console.Write(node.Value + " "); Preorder(node.Left); Preorder(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(10); tree.Root.Left = new Node(5); tree.Root.Right = new Node(15); tree.Root.Left.Left = new Node(3); tree.Root.Left.Right = new Node(7); tree.Root.Right.Right = new Node(18); tree.Preorder(tree.Root);
Attempts:
2 left
💡 Hint
Preorder traversal visits the root node first, then the left subtree, then the right subtree.
✗ Incorrect
Preorder traversal prints the root node first, then recursively visits the left subtree, then the right subtree. So the order is 10 (root), then left subtree 5, 3, 7, then right subtree 15, 18.
❓ Predict Output
intermediateWhat is the output after inserting nodes manually?
Given the following code that manually creates a binary tree, what will be the output of the inorder traversal (left, root, right)?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } public class BinaryTree { public Node Root; public void Inorder(Node node) { if (node == null) return; Inorder(node.Left); System.Console.Write(node.Value + " "); Inorder(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(8); tree.Root.Left = new Node(3); tree.Root.Right = new Node(10); tree.Root.Left.Left = new Node(1); tree.Root.Left.Right = new Node(6); tree.Root.Right.Right = new Node(14); tree.Inorder(tree.Root);
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree first, then root, then right subtree.
✗ Incorrect
Inorder traversal prints nodes in ascending order for a binary search tree. The order is 1, 3, 6, 8, 10, 14.
🔧 Debug
advancedWhy does this manual binary tree creation cause a NullReferenceException?
Examine the following code that tries to create a binary tree manually. Why does it cause a NullReferenceException when calling Preorder traversal?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; } } public class BinaryTree { public Node Root; public void Preorder(Node node) { if (node == null) return; System.Console.Write(node.Value + " "); Preorder(node.Left); Preorder(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(20); tree.Root.Left.Value = 10; tree.Root.Right = new Node(30); tree.Preorder(tree.Root);
Attempts:
2 left
💡 Hint
Check if all child nodes are initialized before accessing their properties.
✗ Incorrect
tree.Root.Left is null by default. Trying to access tree.Root.Left.Value causes NullReferenceException because Left node was never created.
🧠 Conceptual
advancedHow many nodes are in this manually created binary tree?
Given the following manual binary tree creation code, how many nodes does the tree contain?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } var root = new Node(1); root.Left = new Node(2); root.Right = new Node(3); root.Left.Left = new Node(4); root.Left.Right = new Node(5); root.Right.Left = new Node(6); root.Right.Right = new Node(7); root.Left.Left.Left = new Node(8);
Attempts:
2 left
💡 Hint
Count each node created with new Node(...)
✗ Incorrect
Nodes created: 1,2,3,4,5,6,7,8 total 8 nodes.
🚀 Application
expertWhat is the output of this manual binary tree level order traversal?
Given the following code that manually creates a binary tree and performs level order traversal (breadth-first), what will be printed?
DSA C#
using System.Collections.Generic; public class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } public class BinaryTree { public Node Root; public void LevelOrder() { if (Root == null) return; Queue<Node> queue = new Queue<Node>(); queue.Enqueue(Root); while (queue.Count > 0) { Node current = queue.Dequeue(); System.Console.Write(current.Value + " "); if (current.Left != null) queue.Enqueue(current.Left); if (current.Right != null) queue.Enqueue(current.Right); } } } var tree = new BinaryTree(); tree.Root = new Node(1); tree.Root.Left = new Node(2); tree.Root.Right = new Node(3); tree.Root.Left.Left = new Node(4); tree.Root.Left.Right = new Node(5); tree.Root.Right.Right = new Node(6); tree.LevelOrder();
Attempts:
2 left
💡 Hint
Level order traversal visits nodes level by level from left to right.
✗ Incorrect
Level 1: 1
Level 2: 2, 3
Level 3: 4, 5, 6
So output is 1 2 3 4 5 6
