Challenge - 5 Problems
Binary Tree Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Tree Node Creation
What is the output of the following C# code that creates a simple binary tree node and prints its value?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int value) { val = value; left = null; right = null; } } TreeNode root = new TreeNode(10); Console.WriteLine(root.val);
Attempts:
2 left
💡 Hint
Check the value assigned to the root node and what is printed.
✗ Incorrect
The TreeNode constructor sets val to 10. Printing root.val outputs 10.
❓ Predict Output
intermediateValue of Left Child Node
Given the following code, what is the value printed for the left child node?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int value) { val = value; left = null; right = null; } } TreeNode root = new TreeNode(5); root.left = new TreeNode(3); Console.WriteLine(root.left.val);
Attempts:
2 left
💡 Hint
Look at the value assigned to root.left.
✗ Incorrect
The left child node is created with value 3, so printing root.left.val outputs 3.
🔧 Debug
advancedIdentify the Error in TreeNode Initialization
What error will this code produce when compiled and run?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int value) { val = value; left = null; right = null; } } TreeNode root = new TreeNode();
Attempts:
2 left
💡 Hint
Check the constructor parameters and how the object is created.
✗ Incorrect
The TreeNode class has a constructor that requires an int argument. Calling new TreeNode() without arguments causes a compilation error.
🧠 Conceptual
advancedBinary Tree Node Left and Right Initialization
In a binary tree node class, what is the default value of the left and right child nodes if they are not explicitly assigned?
Attempts:
2 left
💡 Hint
Consider what happens to reference type fields when not assigned.
✗ Incorrect
In C#, reference type fields like left and right default to null if not assigned.
❓ Predict Output
expertOutput After Multiple Node Assignments
What is the output of the following code snippet?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int value) { val = value; left = null; right = null; } } TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); Console.WriteLine(root.left.right.val);
Attempts:
2 left
💡 Hint
Trace the assignments from root to left child to right child.
✗ Incorrect
root.left.right is assigned a TreeNode with value 5, so printing its val outputs 5.
