Binary Tree Algorithms
- 26Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is a leaf node in a binary tree?
What is a leaf node in a binary tree?
26 questions
- 1
What is a leaf node in a binary tree?
- A
A node with no parent
- B
A node that has exactly one child
- C
A node that is the topmost node
- D
A node with no children
Correct answer
DA node with no children
Explanation
A leaf node is defined as a node that has zero children (no outgoing arrows). The root is the node with no parent, and internal nodes have at least one child.
- A
- 2
In binary tree terminology, what is an edge?
- A
A node that has two children
- B
The topmost node of the tree
- C
The value stored in a node
- D
The line or arrow connecting two nodes
Correct answer
DThe line or arrow connecting two nodes
Explanation
Edges represent the parent-child relationships between nodes, drawn as lines or arrows connecting them.
- A
- 3
Which node is considered the root of a binary tree?
- A
The bottommost node
- B
The node with no parent
- C
The node with the most children
- D
The node with no children
Correct answer
BThe node with no parent
Explanation
The root is the only node that has no parent (no incoming arrows). It is typically drawn at the top.
- A
- 4
Which of the following is NOT a requirement for a binary tree?
- A
There is exactly one root node
- B
Each node has at most two children
- C
Every node must have two children
- D
There is exactly one path from the root to any node
Correct answer
CEvery node must have two children
Explanation
A binary tree only requires nodes to have at most two children (0, 1, or 2). Nodes are not required to have two children.
- A
- 5
Is a singleton tree (a single node with no children) considered a binary tree?
- A
Yes, but only if it has both left and right children as null
- B
Yes, because it has a root and each node has at most two children
- C
No, because it does not have a path from root to leaf
- D
No, because it does not have any leaves
Correct answer
BYes, because it has a root and each node has at most two children
Explanation
A single node meets all binary tree criteria: one root, at most two children (0), and a trivial path to itself.
- A
- 6
Is the empty tree (zero nodes) considered a binary tree?
- A
No, a binary tree must have at least one node
- B
Yes, but only if it is explicitly defined as null
- C
Yes, it is a special case and is useful for base cases in algorithms
- D
No, because it has no root
Correct answer
CYes, it is a special case and is useful for base cases in algorithms
Explanation
The empty tree is treated as a binary tree to simplify recursive base cases, e.g., returning 0 for sum or false for search.
- A
- 7
What distinguishes a binary tree from a general tree?
- A
Each node has at most two children
- B
The tree has a single root
- C
All nodes store numbers
- D
Each node has exactly one parent
Correct answer
AEach node has at most two children
Explanation
In a binary tree, each node can have at most two children (left and right). General trees allow any number of children.
- A
- 8
In a binary tree representation using nodes, what does a null pointer for a child indicate?
- A
The tree is empty
- B
The node is the root
- C
The node has no value
- D
That child does not exist
Correct answer
DThat child does not exist
Explanation
Null is used to indicate that the node does not have a left or right child. The actual node value is stored in a separate field (e.g., val).
- A
- 9
Which of the following structures would NOT be a binary tree? (Assume A, B, C are nodes; arrows indicate parent->child)
- A
Only a single node A
- B
A has children B and C; B has child D; D has child E
- C
A has child B; B has child C; C has child A (cycle)
- D
A has children B and C; B has children D and E; C has child F
Correct answer
CA has child B; B has child C; C has child A (cycle)
Explanation
Option 2 has a cycle, so there is no unique root and infinite paths. A valid binary tree must be acyclic with exactly one root.
- A
- 10
In the node class for a binary tree, which properties are typically stored?
- A
value, next, prev
- B
val, left, right
- C
key, leftChild, rightChild, parent
- D
data, parent, children
Correct answer
Bval, left, right
Explanation
The standard node object contains a value (val) and two pointers to its left and right children, which are initially null.
- A
- 11
What data structure is used for an iterative depth-first traversal of a binary tree?
- A
A priority queue
- B
A stack
- C
A hash table
- D
A queue
Correct answer
BA stack
Explanation
Depth-first traversal uses a stack (LIFO) to ensure we go deep before wide. Nodes are popped, processed, and their children are pushed.
- A
- 12
In an iterative depth-first traversal, if we want to visit the left subtree before the right, in which order should we push children onto the stack?
- A
Push right child first, then left child
- B
Push both children simultaneously
- C
The order does not matter
- D
Push left child first, then right child
Correct answer
APush right child first, then left child
Explanation
To favor left, push the right child first and then the left child. The left child will be on top of the stack and processed next.
- A
- 13
Given a binary tree with root A, left child B, right child C, where B has left D and right E, and C has right F, what is the depth-first (left-first) traversal order?
- A
A, B, C, D, E, F
- B
A, B, E, D, C, F
- C
A, B, D, E, C, F
- D
A, C, F, B, E, D
Correct answer
CA, B, D, E, C, F
Explanation
Depth-first left-first: start at A, go to B, then left to D (deepest), back to E, then to C, then right to F. Result: A, B, D, E, C, F.
- A
- 14
What data structure is used for an iterative breadth-first traversal of a binary tree?
- A
A stack
- B
A queue
- C
A set
- D
A map
Correct answer
BA queue
Explanation
Breadth-first traversal uses a queue (FIFO) to process nodes level by level. Nodes are enqueued when visited and dequeued for processing.
- A
- 15
Can breadth-first traversal of a binary tree be easily implemented recursively?
- A
Yes, using a recursive function that calls itself on children
- B
Yes, but only if the tree is balanced
- C
No, because recursion uses a stack, not a queue
- D
No, because recursion cannot handle multiple children
Correct answer
CNo, because recursion uses a stack, not a queue
Explanation
Recursion inherently uses the call stack (LIFO), which contradicts the FIFO nature of a queue needed for breadth-first order. Therefore, iterative implementation is standard.
- A
- 16
Given the same tree as above, what is the breadth-first (left-to-right) traversal order?
- A
A, C, B, D, E, F
- B
A, B, C, D, F, E
- C
A, B, C, D, E, F
- D
A, B, D, E, C, F
Correct answer
CA, B, C, D, E, F
Explanation
Breadth-first visits level by level: Level 0: A; Level 1: B, C; Level 2: D, E, F. So order: A, B, C, D, E, F.
- A
- 17
What is the time complexity of a depth-first traversal on a binary tree with n nodes?
- A
O(log n)
- B
O(n^2)
- C
O(2^n)
- D
O(n)
Correct answer
DO(n)
Explanation
Each node is visited exactly once, and stack operations are O(1) per node. Thus overall O(n).
- A
- 18
What is the space complexity (auxiliary space) of an iterative depth-first traversal in the worst case?
- A
O(1)
- B
O(n^2)
- C
O(log n)
- D
O(n)
Correct answer
DO(n)
Explanation
In a skewed tree, the stack can hold up to n nodes (e.g., a chain), making worst-case space O(n).
- A
- 19
In the tree includes problem (search for a target value), if using a recursive depth-first approach, what logical operation is used to combine results from left and right subtrees?
- A
AND
- B
Addition
- C
OR
- D
XOR
Correct answer
COR
Explanation
The tree includes function returns true if the target is found in the left subtree OR the right subtree (or the current node). The OR operator combines Boolean results.
- A
- 20
In the recursive tree sum problem, what is the base case for a null node?
- A
Return null
- B
Return 0
- C
Return Infinity
- D
Return the node's value
Correct answer
BReturn 0
Explanation
An empty subtree contributes nothing to the sum, so the sum of a null node is 0.
- A
- 21
In the recursive tree min value problem, what should be returned for a null node?
- A
0
- B
null
- C
Positive Infinity
- D
Negative Infinity
Correct answer
CPositive Infinity
Explanation
Returning positive infinity ensures that null nodes do not affect the minimum, because any actual number is smaller.
- A
- 22
In the recursive tree min value problem, how is the minimum value computed for a non-null node?
- A
Return min(node.val, leftMin, rightMin)
- B
Return node.val + leftMin + rightMin
- C
Return max(node.val, leftMin, rightMin)
- D
Return node.val if node.val < leftMin else leftMin
Correct answer
AReturn min(node.val, leftMin, rightMin)
Explanation
The minimum of the tree rooted at node is the smallest among its own value and the minima of its left and right subtrees.
- A
- 23
In the max root-to-leaf path sum problem, what value should be returned for a null node?
- A
The node's value
- B
0
- C
Positive infinity
- D
Negative infinity
Correct answer
DNegative infinity
Explanation
Negative infinity ensures that null nodes are ignored when taking the maximum, as any real number is greater.
- A
- 24
In the max root-to-leaf path sum problem, what is the base case for a leaf node?
- A
Return 0
- B
Return negative infinity
- C
Return positive infinity
- D
Return the node's value
Correct answer
DReturn the node's value
Explanation
A leaf node has no children, so the maximum path from that node to itself is just its own value.
- A
- 25
Which traversal order is used for the max root-to-leaf path sum recursive solution?
- A
Depth-first (pre-order)
- B
Depth-first (post-order)
- C
Breadth-first
- D
In-order
Correct answer
BDepth-first (post-order)
Explanation
The recursion first computes results for left and right subtrees (children) before combining them with the current node's value, which is post-order DFS.
- A
- 26
In the tree includes iterative breadth-first approach, when should we check if the current node's value matches the target?
- A
When the node is added to the queue
- B
At the start of each level
- C
Only after the entire traversal
- D
When the node leaves the queue
Correct answer
DWhen the node leaves the queue
Explanation
The standard pattern is to process a node when it is dequeued (leaves the queue) and check for equality at that point.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.