📖 Scenario: Imagine you are building a simple family tree app. Each person can have up to two children. To start, you need to create the basic building block: a node that holds a person's name and links to their children.
🎯 Goal: Create a binary tree node structure in C# that can hold a person's name and references to their left and right children.
📋 What You'll Learn
Create a class called TreeNode
Add a public string variable called Name to store the person's name
Add two public variables called Left and Right of type TreeNode to link to children
Create a constructor that takes a string parameter to set the Name
💡 Why This Matters
🌍 Real World
Binary trees are used in many apps like family trees, decision trees, and organizing data hierarchically.
💼 Career
Understanding how to build and use tree nodes is essential for software developers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the TreeNode class with Name variable
Create a public class called TreeNode with a public string variable called Name.
DSA C#
Hint
Start by defining a class named TreeNode. Inside it, add a public string variable called Name to hold the person's name.
2
Add Left and Right child references
Inside the TreeNode class, add two public variables called Left and Right of type TreeNode to represent the left and right children.
DSA C#
Hint
Think of Left and Right as pointers to the person's children. They should be of the same type as the class itself.
3
Add constructor to set Name
Add a public constructor to the TreeNode class that takes a string parameter called name and sets the Name variable.
DSA C#
Hint
The constructor helps create a new node with a name already set. Use the parameter name to assign the Name variable.
4
Create a TreeNode instance and print Name
Create a TreeNode object called root with the name "Grandparent". Then print the Name of root using Console.WriteLine.
DSA C#
Hint
Create the root node with the name "Grandparent" and print its name to see the output.
Practice
(1/5)
1. What does a typical binary tree node contain in its structure?
easy
A. Only a value without any references
B. A value and a reference to the next node in a list
C. References to parent and sibling nodes only
D. A value and references to left and right child nodes
Solution
Step 1: Understand binary tree node components
A binary tree node stores data and links to its children nodes.
Step 2: Identify correct node structure
It must have a value and two references: one for left child and one for right child.
Final Answer:
A value and references to left and right child nodes -> Option D
Quick Check:
Binary tree node = value + left + right [OK]
Hint: Remember: binary tree nodes have two child links [OK]
Common Mistakes:
Thinking nodes only store values without links
Confusing with linked list node structure
Assuming nodes have parent or sibling references by default
2. Which of the following is the correct C# syntax to declare a binary tree node class with integer value and left/right children?
easy
A. public struct Node { int val; Node left; Node right; }
B. class Node { int val; Node left, right; }
C. public class Node { public int val; public Node left; public Node right; }
D. public class Node { int val; Node left; Node right; void Node() {} }
Solution
Step 1: Check access modifiers and field declarations
Public class with public fields allows access outside the class, correct for node structure.
Step 2: Validate syntax correctness
public class Node { public int val; public Node left; public Node right; } uses public class, public int val, and public Node left/right fields correctly.
Final Answer:
public class Node { public int val; public Node left; public Node right; } -> Option C
Quick Check:
Public class with public fields = public class Node { public int val; public Node left; public Node right; } [OK]
Hint: Use public class and public fields for node structure [OK]
Common Mistakes:
Missing public keyword causing access issues
Using struct instead of class for nodes
Incorrect constructor syntax or missing access modifiers
3. Given the following code, what will be the output when printing the root node's left child's value?
public class Node {
public int val;
public Node left;
public Node right;
public Node(int v) { val = v; }
}
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
Console.WriteLine(root.left.val);
medium
A. 5
B. null
C. 15
D. 10
Solution
Step 1: Trace node assignments
Root node value is 10, left child assigned new Node with value 5.
Step 2: Identify printed value
Printing root.left.val outputs 5, the left child's value.
Final Answer:
5 -> Option A
Quick Check:
root.left.val = 5 [OK]
Hint: Print left child's val to get 5 [OK]
Common Mistakes:
Printing root.val instead of root.left.val
Confusing left and right child values
Assuming uninitialized children print null
4. Identify the error in the following binary tree node class code:
public class Node {
public int val;
public Node left, right;
public Node(int value) {
val = value;
left = null;
right = null;
}
}
Node root = new Node(10);
root.left.val = 5;
medium
A. Cannot assign val to left child before initializing left node
B. Constructor missing return type
C. left and right should be private fields
D. Node class should be a struct
Solution
Step 1: Analyze node initialization
left and right are initialized to null in constructor, so root.left is null initially.
Step 2: Check assignment to left.val
Trying to assign root.left.val = 5 causes null reference error because root.left is null.
Final Answer:
Cannot assign val to left child before initializing left node -> Option A
Quick Check:
Assigning to null child causes error [OK]
Hint: Initialize child node before assigning its value [OK]
Common Mistakes:
Assuming child nodes auto-initialize
Ignoring null reference exceptions
Confusing constructor syntax errors
5. How would you modify the binary tree node class to include a method that returns true if the node is a leaf (has no children)?
hard
A. public bool IsLeaf() { return val == null; }
B. public bool IsLeaf() { return left == null && right == null; }
C. public bool IsLeaf() { return left != null || right != null; }
D. public bool IsLeaf() { return left == right; }
Solution
Step 1: Define leaf node condition
A leaf node has no left or right children, so both must be null.
Step 2: Write method to check children
Return true if left and right are null, false otherwise.
Final Answer:
public bool IsLeaf() { return left == null && right == null; } -> Option B
Quick Check:
Leaf check = left and right null [OK]
Hint: Leaf means no children: left and right null [OK]
Common Mistakes:
Checking value instead of children
Using OR instead of AND in condition
Comparing left and right references directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.