Binary Tree Algorithms
- 30Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Define what a tree is in computer science terms.
30 questions
- 1
Define what a tree is in computer science terms.
Model answer
A tree consists of nodes (circles) and edges (arrows) connecting them.
Explanation
Nodes store values; edges represent relationships between nodes.
- 2
Explain the parent-child relationship in a tree.
Model answer
A parent node points to its children nodes; children are nodes that have the parent as their direct ancestor.
Explanation
Familial terminology helps describe the hierarchical structure of a tree.
- 3
What is a root node in a tree?
Model answer
The root is the node that has no parent (no incoming edges).
Explanation
Typically drawn as the topmost node; a binary tree has exactly one root.
- 4
What is a leaf node in a tree?
Model answer
A leaf node is a node with no children (no outgoing edges).
Explanation
Leaves are endpoints of the tree; they can occur at different levels.
- 5
Can a node be both root and leaf? Give an example.
Model answer
Yes, a singleton tree (single node) is both root and leaf because it has no parent and no children.
Explanation
In a tree with one node, that node satisfies both definitions.
- 6
State the three criteria for a binary tree.
Model answer
(1) Every node has at most two children; (2) there is exactly one root; (3) there is exactly one path between the root and any node.
Explanation
These define the structure of a binary tree.
- 7
Is a tree with a node having three children a binary tree? Why?
Model answer
No, because the first criterion requires at most two children per node.
Explanation
Binary refers to two; a tree with up to three children is a ternary tree.
- 8
Is a tree with two nodes that both have no parent a binary tree? Why?
Model answer
No, because there would be two roots, violating the exactly one root criterion.
Explanation
A binary tree has a single root node with no parent.
- 9
Is a tree with a cycle (e.g., A→B→C→A) a binary tree? Why?
Model answer
No, because there are multiple paths between nodes (and no unique root).
Explanation
Cycles break the unique path requirement and also cause nodes to have parents when they shouldn't.
- 10
Is an empty tree (zero nodes) considered a binary tree?
Model answer
Yes, it is considered a binary tree (a valid edge case).
Explanation
Many algorithms treat null as representing an empty binary tree.
- 11
How is a binary tree node typically represented programmatically?
Model answer
As a class/object with properties: val (value), left (reference to left child), right (reference to right child). Left and right are initialized to null.
Explanation
This allows building tree structure by assigning children.
- 12
In the Node class, what do null values for left and right indicate?
Model answer
They indicate that the node does not have that child (child is absent).
Explanation
Leaf nodes have both left and right as null.
- 13
What data structure is used for an iterative depth-first traversal? Why?
Model answer
A stack, because depth-first goes deep first and the LIFO property of a stack matches that order.
Explanation
Nodes are pushed onto the stack; when popped, we process the node and push its children.
- 14
In an iterative depth-first traversal that processes left before right, in what order should children be pushed?
Model answer
Push the right child first, then the left child, so that the left child is popped next.
Explanation
Stack LIFO means later push is popped first; pushing right then left yields left-first processing.
- 15
When should a node's value be added to the result list in iterative depth-first traversal?
Model answer
When the node is popped from the stack (i.e., when it becomes current).
Explanation
That is when the node is visited.
- 16
Describe the base case for a recursive depth-first traversal that returns an array of all values.
Model answer
If the root is null, return an empty array.
Explanation
An empty tree has no values; otherwise, return [root.val] concatenated with left and right results.
- 17
What is the time complexity of depth-first traversal (iterative or recursive) on a tree with n nodes?
Model answer
O(n) time, because each node is visited exactly once.
Explanation
Each node is pushed and popped once (or called recursively once).
- 18
What is the space complexity of recursive depth-first traversal in the worst case?
Model answer
O(n) space due to the call stack in the worst case (skewed tree).
Explanation
Recursion depth equals tree height, which can be n in worst case.
- 19
What data structure is used for breadth-first traversal? Why?
Model answer
A queue, because breadth-first processes nodes level by level, and the FIFO property of a queue ensures fair ordering.
Explanation
Nodes are enqueued; when dequeued, we process and enqueue children.
- 20
In breadth-first traversal, when should a node's value be recorded?
Model answer
When the node is dequeued (removed from the front of the queue).
Explanation
That is when the node is visited.
- 21
Why is it difficult to implement breadth-first traversal recursively?
Model answer
Recursion relies on a stack (call stack), which gives depth-first behavior, not level-order.
Explanation
BFS requires a queue structure, which is unnatural for recursion.
- 22
What is the time complexity of breadth-first traversal assuming efficient queue operations?
Model answer
O(n) time, since each node is enqueued and dequeued once.
Explanation
With O(1) enqueue/dequeue, total time linear.
- 23
In the tree includes problem, how can you determine if a target value exists in the tree?
Model answer
Perform a traversal (DFS or BFS) and check each node's value; return true if found, false if traversal completes without finding.
Explanation
All nodes must be visited unless early exit.
- 24
Write the recursive logic for tree includes.
Model answer
Base case: if root is null, return false. If root.val equals target, return true. Otherwise, return treeIncludes(root.left, target) OR treeIncludes(root.right, target).
Explanation
Uses boolean OR to combine results from subtrees.
- 25
In the recursive tree includes, what is the purpose of the null base case?
Model answer
To return false when we reach a non-existent child, indicating the target is not in that subtree.
Explanation
An empty subtree cannot contain the value.
- 26
Write the recursive logic for tree sum.
Model answer
Base case: if root is null, return 0. Otherwise, return root.val + treeSum(root.left) + treeSum(root.right).
Explanation
Sum of current node plus sums of left and right subtrees.
- 27
In the recursive tree min value function, what should a null node return and why?
Model answer
Positive Infinity, because when comparing with actual values, infinity will never be chosen as the minimum.
Explanation
This ensures null nodes don't affect the min computation.
- 28
Write the recursive logic for tree min value.
Model answer
Base case: if root is null, return Infinity. Otherwise, return Math.min(root.val, treeMin(root.left), treeMin(root.right)).
Explanation
The minimum of the current node and the minima of its subtrees.
- 29
For max root-to-leaf path sum, what are the two base cases and what do they return?
Model answer
If root is null, return -Infinity. If root is a leaf (no children), return root.val.
Explanation
Null nodes contribute nothing; leaves return their own value as the path sum.
- 30
Write the recursive logic for max root-to-leaf path sum.
Model answer
If root is null, return -Infinity. If leaf, return root.val. Otherwise, return root.val + Math.max(maxPathSum(root.left), maxPathSum(root.right)).
Explanation
Choose the larger path from left or right and add current node's value.

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