A binary tree node is a basic unit of a binary tree that contains data and references to two child nodes: left and right.
Click to reveal answer
beginner
What are the main parts of a binary tree node?
A binary tree node has three main parts: 1. Data (value stored) 2. Left child reference 3. Right child reference
Click to reveal answer
beginner
Show a simple Java class for a binary tree node.
public class TreeNode {<br> int data;<br> TreeNode left;<br> TreeNode right;<br><br> public TreeNode(int data) {<br> this.data = data;<br> this.left = null;<br> this.right = null;<br> }<br>}
Click to reveal answer
beginner
Why do binary tree nodes have two child references?
Because each node can have up to two children: a left child and a right child, which helps form the tree structure.
Click to reveal answer
beginner
What does it mean if a binary tree node's left or right child is null?
It means that the node does not have a child on that side, so the branch ends there.
Click to reveal answer
What does a binary tree node typically contain?
AOnly two child references
BOnly data
CData and three child references
DData and two child references
✗ Incorrect
A binary tree node contains data and references to two children: left and right.
In Java, what type are the left and right child references in a binary tree node?
Aint
BString
CTreeNode
Dboolean
✗ Incorrect
The left and right child references are of type TreeNode, pointing to other nodes.
What does it mean if a binary tree node's left child is null?
AThe node has no left child
BThe node has a left child
CThe node is the root
DThe node has two children
✗ Incorrect
A null left child means no left child exists for that node.
How many children can a binary tree node have at most?
AOne
BTwo
CThree
DUnlimited
✗ Incorrect
A binary tree node can have up to two children: left and right.
Which of these is NOT part of a binary tree node?
AParent reference
BData value
CRight child reference
DLeft child reference
✗ Incorrect
A basic binary tree node does not usually have a parent reference.
Describe the structure of a binary tree node and its components.
Think about what each node stores and how it connects to others.
You got /4 concepts.
Explain why binary tree nodes have two child references and what it means when they are null.
Consider how a tree branches out from each node.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a TreeNode class in a binary tree?
easy
A. To perform tree traversal operations
B. To store only the root node of the tree
C. To store a value and references to left and right child nodes
D. To balance the binary tree automatically
Solution
Step 1: Understand the role of a node in a binary tree
A node holds data and links to its children, enabling tree structure.
Step 2: Identify what the TreeNode class stores
It stores a value and references (pointers) to left and right child nodes.
Final Answer:
To store a value and references to left and right child nodes -> Option C
Quick Check:
Node = value + left + right [OK]
Hint: Nodes hold data and links to children [OK]
Common Mistakes:
Thinking TreeNode stores the whole tree
Confusing node with traversal logic
Assuming TreeNode balances the tree
2. Which of the following is the correct Java syntax to define a binary tree node class with integer data?
easy
A. class TreeNode { int data; TreeNode left; TreeNode right; }
B. class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int val) { data = val; } }
C. class TreeNode { int data; TreeNode left; TreeNode right; void TreeNode() {} }
D. class TreeNode { int data; TreeNode left, right; }
Solution
Step 1: Check class field declarations
Fields for data, left, and right nodes must be declared properly.
Step 2: Verify constructor syntax
Constructor should initialize data; class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int val) { data = val; } } correctly defines constructor.
Final Answer:
class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int val) { data = val; } } -> Option B
Quick Check:
Constructor initializes data correctly [OK]
Hint: Constructor must initialize data field [OK]
Common Mistakes:
Missing constructor to set data
Incorrect constructor syntax (void TreeNode())
Declaring multiple variables incorrectly
3. Given the following code, what will be printed?
class TreeNode {
int data;
TreeNode left, right;
TreeNode(int val) { data = val; }
}
public class Main {
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
System.out.println(root.left.data + "," + root.right.data);
}
}
medium
A. 5,15
B. 10,5
C. 15,5
D. 10,15
Solution
Step 1: Create root node with data 10
Root node data is 10, left and right are null initially.
Step 2: Assign left and right children with data 5 and 15
root.left.data = 5, root.right.data = 15.
Step 3: Print left and right data
Output is "5,15" from root.left.data and root.right.data.
Final Answer:
5,15 -> Option A
Quick Check:
Left=5, Right=15 [OK]
Hint: Print left and right node data directly [OK]
Common Mistakes:
Printing root data instead of children
Swapping left and right values
Confusing node references
4. Identify the error in the following TreeNode class code:
class TreeNode {
int data;
TreeNode left, right;
void TreeNode(int val) {
data = val;
}
}
medium
A. Constructor has void return type, so it's a method not a constructor
B. Missing semicolon after data declaration
C. left and right should be arrays, not single nodes
D. Class name and constructor name do not match
Solution
Step 1: Check constructor syntax
Constructor should not have a return type; 'void' makes it a method.
Step 2: Confirm class and constructor names match
Names match, so no error there.
Final Answer:
Constructor has void return type, so it's a method not a constructor -> Option A
Quick Check:
Constructor no return type [OK]
Hint: Constructor must not have a return type [OK]
Common Mistakes:
Adding void before constructor name
Confusing methods with constructors
Thinking left/right must be arrays
5. You want to create a binary tree node class that also stores a parent node reference. Which of the following class definitions correctly adds this feature?
hard
A. class TreeNode { int data; TreeNode left, right, parent; TreeNode(int val) { data = val; } }
B. class TreeNode { int data; TreeNode left, right; TreeNode parent; TreeNode(int val) { data = val; parent = null; } }
C. class TreeNode { int data; TreeNode left, right; TreeNode parent; TreeNode(int val) { data = val; left = right = parent = null; } }
D. class TreeNode { int data; TreeNode left, right; TreeNode parent; TreeNode(int val, TreeNode p) { data = val; parent = p; } }
Solution
Step 1: Understand the need for parent reference initialization
Parent reference should be set when node is created to link properly.
Step 2: Check constructor parameters and assignments
class TreeNode { int data; TreeNode left, right; TreeNode parent; TreeNode(int val, TreeNode p) { data = val; parent = p; } } passes parent node as parameter and assigns it correctly.
Final Answer:
class TreeNode { int data; TreeNode left, right; TreeNode parent; TreeNode(int val, TreeNode p) { data = val; parent = p; } } -> Option D
Quick Check:
Parent passed and assigned in constructor [OK]
Hint: Pass parent node in constructor to set reference [OK]
Common Mistakes:
Not initializing parent reference
Setting parent to null always
Declaring parent but never assigning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.