Matching Phone Menus
A clinic runs two phone lines. Callers hear a numbered recording, press 1 or 2, hear the next, and so on until a final message. Each line's menu is therefore a binary tree, where pressing 1 goes left and pressing 2 goes right.
The menus should be copies of each other. After a messy update, the manager wants to know whether they still are, meaning the same recording plays at every step and the same keys lead to the same places.
Your task
Write sameMenu(first, second). Return true if the trees are identical: same shape and same value in every matching position. Otherwise return false.
Two empty trees are identical. Trees are shown in level order, like [7, null, 1], with null for a missing child. Your function receives real TreeNode objects (or null) with val, left and right.
sameMenu(first: TreeNode, second: TreeNode) → bool
Examples
first = [4, 2, 6, 1, 3] second = [4, 2, 6, 1, 3]
true
Both start with 4. Press 1 gives 2 on both, press 2 gives 6 on both, and below 2 the keys lead to 1 and 3 on both.
first = [7, 1] second = [7, null, 1]
false
Both contain 7 and 1, but on the first line you reach 1 by pressing 1, on the second by pressing 2. The menus are different, so the answer is false.
Constraints
- 0 ≤ number of messages in each menu ≤ 1,000
- -10,000 ≤ recording number ≤ 10,000
- Recording numbers may repeat. Each tree is at most 500 levels deep.
Start with brute force
Write each tree out as a list and compare. A plain in-order or pre-order list of values is wrong: [7, 1] and [7, null, 1] both give pre-order 7, 1, yet are different menus. The list must record missing children too, as level order does.
With nulls included it is correct and O(n), but it builds two full lists first. Walking both trees side by side needs no lists and stops at the first difference.
Hints
0 of 4 shown- Hint 1
In the second example both menus hold the same two recordings. What exactly differs, and would a list of all recordings notice?
- Hint 2
Two menus match when their first recordings match, their press-1 menus match, and their press-2 menus match. The last two are the same question on smaller trees.
- Hint 3
What are the stopping cases? What should happen when both sub-menus are missing, and what when only one is?
- Hint 4
Recurse on both trees at once. If both are null, return true, and if exactly one is null, return false. Otherwise return true only when
a.val === b.valandsame(a.left, b.left)andsame(a.right, b.right)are all true.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Walk both menus together, pressing the same key on both. Each step has three possible outcomes. If both lines have ended, that part matches. If only one has ended, the shapes differ. If both play a recording, compare the two values and then continue on both keys.
Trace [4, 2, 6, 1, 3] against [4, 2, 6, null, 3]:
- The roots 4 and 4 match, so press 1 on both.
- The next recordings 2 and 2 match, so press 1 on both.
- The first line plays 1 and the second has nothing, so exactly one is null and the answer is false.
The && (and) operator stops early, so once a comparison is false the rest of the tree is never visited.
function sameMenu(first, second) {
if (first === null && second === null) return true; // both ended
if (first === null || second === null) return false; // only one ended
return first.val === second.val
&& sameMenu(first.left, second.left)
&& sameMenu(first.right, second.right);
}def same_menu(first, second):
if first is None and second is None: # both ended
return True
if first is None or second is None: # only one ended
return False
return (first.val == second.val
and same_menu(first.left, second.left)
and same_menu(first.right, second.right))Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.