Binary Trees
DFS traversals, tree height, path sums, LCA, and level-order BFS — all the core binary tree patterns.
Questions in this quiz
10 questions on Binary Trees. Work through them here, then start the quiz to answer them and see your score.
1.Inorder traversal of a Binary Search Tree (BST) gives what?
- AValues in ascending (sorted) order
- BValues in descending order
- CA random ordering of the values
- DA level-by-level listing
2.Which traversal order should you use to delete all nodes of a tree without losing references to children?
- ALevel order (BFS)
- BPreorder (root, left, right)
- CPostorder (left, right, root)
- DInorder (left, root, right)
3.What does this function return for a tree with root=1, left=2, right=3 (no further children)?
public int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); }- A2
- B0
- C3
- D1
4.Why is it WRONG to check if a tree is a valid BST by only comparing each node to its immediate parent?
- AIt is not wrong — parent comparison is sufficient
- BBST nodes must also be balanced
- CParent comparisons are too slow
- DA node in the left subtree could be greater than a grandparent ancestor, violating the BST property
5.For the tree 3 → (9, 20 → (15, 7)), what does BFS level-order traversal return?
Queue<TreeNode> q = new LinkedList<>(); q.offer(root); while (!q.isEmpty()) { int size = q.size(); // capture level size List<Integer> level = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode node = q.poll(); level.add(node.val); if (node.left != null) q.offer(node.left); if (node.right != null) q.offer(node.right); } result.add(level); }- A[[3, 9, 20, 15, 7]]
- B[[3], [9, 20], [15, 7]]
- C[[3], [9], [20], [15], [7]]
- D[[15, 7], [9, 20], [3]]
6.You want to find all root-to-leaf paths in a binary tree. Which traversal with what extra technique?
- ABFS with a path queue alongside the node queue
- BInorder, recording nodes as visited
- CPostorder, summing values upward
- DPreorder DFS with a path list — add current node, recurse, then remove (backtrack)
7.What is the Lowest Common Ancestor (LCA) of two nodes p and q in a binary tree?
- AThe first node encountered when doing BFS from p
- BThe deepest node that has both p and q as descendants (a node is a descendant of itself)
- CThe parent of the node with the smaller value
- DAlways the root of the tree
8.What is the diameter of a binary tree?
- AThe number of leaf nodes
- BThe length of the longest path between any two nodes (edges), which may not pass through the root
- CThe number of nodes at the deepest level
- DThe height of the tree
9.What is the time and space complexity of DFS tree height (recursive)?
- AO(n) time, O(n) space always
- BO(n) time, O(h) space where h is tree height
- CO(n log n) time, O(1) space
- DO(h) time, O(1) space
10.Path Sum: does a root-to-leaf path exist with a given target? Which traversal and how?
- APreorder DFS — subtract current node's value from target at each step; at a leaf, check if remaining target is 0
- BInorder DFS, maintaining a running sum
- CPostorder DFS, adding values bottom-up
- DLevel order BFS, checking cumulative sums per level