Flip Equivalent Binary Trees
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SambaNova Systems OA reported in October 2021 asks whether two binary trees are flip equivalent, and it's a recursion check dressed up as a tree puzzle. Strip the wording and it's just comparing two trees where you're allowed to swap children at any node. If you've got an invite and 48 hours, this is a ten-line solution once you see it. Trees are tiny, up to 100 nodes, so there's no performance trap. The risk is blanking on the base cases under pressure. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes empty, but you should be able to write this from memory after reading below.
The problem
Two binary trees are flip equivalent if one can be transformed into the other by repeatedly swapping the left and right children of any node. Return whether the two supplied trees are flip equivalent. Function flipEquiv(root1: TreeNode, root2: TreeNode) → boolean Examples Example 1 root1 = [1,2,3,4,5,6,null,null,null,7,8] root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7] return = true Flips at selected nodes align both trees. Example 2 root1 = [] root2 = [] return = true Two empty trees are equivalent. Example 3 root1 = [] root2 = [1] return = false Only one tree is empty. Constraints Each tree has 0 to 100 nodes. 0 <= node.val <= 100.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's what it reduces to: two trees are flip equivalent if the roots match and either the children match straight (left with left, right with right) or crossed (left with right, right with left). That's a recursive function with a few base cases. Both null returns true. Exactly one null returns false. Different values returns false. Otherwise return (f(a.left, b.left) and f(a.right, b.right)) or (f(a.left, b.right) and f(a.right, b.left)). The common pitfall is forgetting the one-null case and crashing on a null dereference, or only checking the crossed case. Values can repeat in general, but since you compare at each node with both options, you don't need to sort children or canonicalize. Depth is at most 100, so recursion is safe. If you freeze on the live OA, StealthCoder can hand you this structure while you stay in control of typing it out.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Flip Equivalent Binary Trees cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as flip equivalent binary trees. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SambaNova Systems's OA.
SambaNova Systems reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Flip Equivalent Binary Trees FAQ
What's the trick to Flip Equivalent Binary Trees?+
Recurse on both trees together. At each pair of nodes, check that values match, then try the children uncrossed and crossed. If either arrangement works for both subtrees, the pair is flip equivalent. Handle null cases first.
How hard is this problem really?+
Easy to medium. The code is short, but candidates trip on base cases. If you can write a same-tree comparison, you're one extra OR branch away from the full answer.
What's the time complexity?+
O(min(n1, n2)) in practice, since each node pair is visited a constant number of times. With values distinct at each level, only one of the two arrangements recurses deeply. Space is O(h) for the recursion stack, at most 100 here.
What edge cases should I test?+
Both trees empty returns true. One empty and one not returns false. Same shape with different values returns false. A single node with matching values returns true. Also test a tree where a flip is needed deep, not just at the root.
How do I prepare in 48 hours for this OA?+
Write same-tree, symmetric-tree, and this one from scratch until the null-check order is automatic. Then do a few more recursive tree problems on paired traversal. That's enough, since this question rewards clean base cases over fancy technique.