Tree coding interview questions

7 coding interview questions on trees, each with the correct answer and a full explanation.

Binary tree level order traversal with a deque. How do you separate levels?
Show answer

Answer

Record len(queue) at the start of each level and pop exactly that many
The queue length at the start of a level equals that level's node count. A None after every node splits per node, not per level. A stack gives DFS order. Incrementing per pop counts nodes, not levels.

Source: LeetCode 102. Binary Tree Level Order Traversal

Validate BST. A solution checks node.left.val < node.val < node.right.val at each node. Which tree breaks it?
Show answer

Answer

5 with left 4 and right 6, where 6 has left child 3
Every local check passes but 3 sits in the right subtree of 5, violating the BST property. Pass lower and upper bounds down instead. The plain 5, 4, 6 tree is a valid BST and passes. A single node and an empty tree are valid.

Source: LeetCode 98. Validate Binary Search Tree

Recursive max depth of a binary tree with n nodes. What is the worst case space?
Show answer

Answer

O(n) for a skewed tree
Recursion depth equals tree height, which is n for a linked list shaped tree. O(log n) holds only when balanced. Recursion is not O(1). No extra log factor applies.

Source: LeetCode 104. Maximum Depth of Binary Tree

Diameter of Binary Tree: longest path between any two nodes, in edges. The helper returns height. What updates the answer at each node?
Show answer

Answer

best = max(best, left_height + right_height)
The longest path through a node joins the two deepest branches. 1 + max is the height, not a path. Only checking the root misses paths that do not pass through it. Multiplying heights has no meaning.

Source: LeetCode 543. Diameter of Binary Tree

Lowest Common Ancestor in a BST for p and q. Best time for a tree of height h?
Show answer

Answer

O(h), walk down using values
BST order says go left if both are smaller, right if both are larger, else stop. That is O(h). Comparing full paths is for general trees. O(log n) assumes balance. Nothing is sorted.

Source: LeetCode 235. Lowest Common Ancestor of a Binary Search Tree

Kth Smallest Element in a BST, k much smaller than n. You want to stop as soon as the kth value is found. Which traversal visits values in sorted order so you can stop at the kth visit?
Show answer

Answer

Inorder
Inorder visits left, node, right, which is ascending in a BST, so stop at the kth visit. Preorder and postorder do not sort. Level order visits by depth.

Source: LeetCode 230. Kth Smallest Element in a BST

Same Tree (check whether two binary trees match in shape and values): give the recursive check and its base cases.
Show answer

Answer

If both are None return True. If one is None or values differ return False. Otherwise return same(p.left, q.left) and same(p.right, q.right). O(n) time, O(h) stack.

Source: LeetCode 100. Same Tree

These questions come from the Coding Interview Practice pack in Bertopia.

More coding topics

Practise these on your commute