The root is the topmost node of a tree. It has no parent and is the starting point of the tree.
Click to reveal answer
beginner
Define a leaf node in a tree.
A leaf node is a node that has no children. It is at the end of a branch.
Click to reveal answer
intermediate
What does the height of a node represent?
Height of a node is the number of edges on the longest path from that node down to a leaf.
Click to reveal answer
intermediate
Explain the depth of a node in a tree.
Depth of a node is the number of edges from the root node down to that node.
Click to reveal answer
beginner
What is the level of a node in a tree?
Level of a node is its depth plus one. The root is at level 1, its children at level 2, and so on.
Click to reveal answer
Which node has no parent in a tree?
ARoot
BLeaf
CChild
DSibling
✗ Incorrect
The root node is the only node without a parent.
A node with no children is called a:
ALeaf
BParent
CRoot
DAncestor
✗ Incorrect
Leaf nodes have no children and are at the ends of branches.
Height of a node is measured by:
AEdges from root to node
BEdges from node to leaf
CNumber of children
DNumber of siblings
✗ Incorrect
Height counts edges on the longest path from the node down to a leaf.
Depth of a node is:
ANumber of children
BEdges from node to leaf
CEdges from root to node
DNumber of leaves
✗ Incorrect
Depth counts edges from the root down to the node.
If the root is at level 1, what level are its children at?
A0
B3
C1
D2
✗ Incorrect
Children of the root are at level 2 (depth 1 + 1).
Describe the difference between depth and height of a node in a tree.
Think about where you start counting edges for each.
You got /4 concepts.
Explain what the level of a node means and how it relates to depth.
Level starts counting from 1 at the root.
You got /4 concepts.
Practice
(1/5)
1. In a tree data structure, which node is called the root?
easy
A. The topmost node with no parent
B. A node with no children
C. Any node at the lowest level
D. A node with exactly two children
Solution
Step 1: Understand the root node definition
The root node is the starting point of a tree and has no parent node above it.
Step 2: Differentiate root from other nodes
Leaf nodes have no children, but root specifically has no parent. Lowest level nodes are leaves or internal nodes, not necessarily root.
Final Answer:
The topmost node with no parent -> Option A
Quick Check:
Root = topmost node with no parent [OK]
Hint: Root is always the top node without a parent [OK]
Common Mistakes:
Confusing root with leaf node
Thinking root must have children
Assuming root is at the lowest level
2. Which of the following is the correct way to declare a tree node struct in Go with integer data and pointers to left and right children?
easy
A. type Node struct { data int, left &Node, right &Node }
B. type Node struct { data int, left *Node, right *Node }
C. type Node struct { data int, left Node, right Node }
D. type Node struct { data int, left Node*, right Node* }
Solution
Step 1: Recall Go pointer syntax
In Go, pointers are declared with * before the type, e.g., *Node.
Step 2: Check each option's pointer usage
type Node struct { data int, left *Node, right *Node } uses *Node correctly. type Node struct { data int, left Node, right Node } uses Node (value, not pointer). type Node struct { data int, left &Node, right &Node } uses &Node which is invalid syntax. type Node struct { data int, left Node*, right Node* } uses Node* which is invalid in Go.
Final Answer:
type Node struct { data int, left *Node, right *Node } -> Option B
Quick Check:
Pointer syntax in Go = *Type [OK]
Hint: Use *Type for pointers in Go structs [OK]
Common Mistakes:
Using value type instead of pointer for children
Using &Type or Type* which are invalid in Go
Using semicolons instead of commas in struct fields
3. Given the following tree structure in Go, what is the depth of node with value 5?
type Node struct {
data int
left *Node
right *Node
}
root := &Node{data: 1}
root.left = &Node{data: 2}
root.right = &Node{data: 3}
root.left.left = &Node{data: 4}
root.left.right = &Node{data: 5}
medium
A. 2
B. 1
C. 3
D. 4
Solution
Step 1: Understand depth definition
Depth of a node is the number of edges from the root to that node.
Step 2: Trace path from root to node 5
Path: 1 (root) -> 2 -> 5. Number of edges = 2.
Final Answer:
2 -> Option A
Quick Check:
Depth = edges from root = 2 [OK]
Hint: Count edges from root to node for depth [OK]
Common Mistakes:
Counting nodes instead of edges
Confusing depth with height
Miscounting path length
4. Consider this Go code snippet for a binary tree node:
type Node struct {
data int
left *Node
right *Node
}
func isLeaf(n *Node) bool {
return n.left == nil && n.right == nil
}
root := &Node{data: 10}
root.left = &Node{data: 20}
root.right = &Node{data: 30}
root.left.left = &Node{data: 40}
fmt.Println(isLeaf(root.left))
What is the output and why?
medium
A. true
B. Runtime panic
C. Compilation error
D. false
Solution
Step 1: Understand isLeaf function
It returns true if both left and right children are nil (no children).
Step 2: Check root.left node children
root.left has left child (40) but no right child, so left != nil, right == nil.
Step 3: Evaluate isLeaf(root.left)
Since left is not nil, isLeaf returns false.
Final Answer:
false -> Option D
Quick Check:
Leaf = no children, root.left has left child [OK]
Hint: Leaf nodes have no children; check both left and right nil [OK]
Common Mistakes:
Assuming node with one child is leaf
Confusing nil checks causing panic
Expecting true without checking both children
5. You have a binary tree where the root is at level 0. If a node is at level 3, what is its height assuming the tree height is 5? Also, which node is at level 0?
Choose the correct pair of answers.
hard
A. Height = 5; Root node is at level 0
B. Height = 3; Leaf node is at level 0
C. Height = 2; Root node is at level 0
D. Height = 2; Leaf node is at level 0
Solution
Step 1: Calculate height of node at level 3
Height = total tree height - node level = 5 - 3 = 2.