Flip Equivalent Binary Trees (LeetCode 951): Solution and Explanation
A flip swaps the two child subtrees of one node. Given two binary trees with unique values, decide whether some set of flips turns the first tree into the second.
Intuition
Flips never change which values sit under which parent. They only change the left-right order of the two children. So two flip equivalent trees must have the same root value, and the same pair of child subtrees under it, in some order.
That gives exactly two ways the children can line up: straight (left with left, right with right) or crossed (left with right, right with left). Choosing crossed at a node is the same as flipping it.
So the check is recursive. Two subtrees match when both are empty, or when their values match and one of the two pairings of their children matches. Because the values are unique, a wrong pairing fails at the very next value comparison, so the search never blows up.
Approach: Recursive pairing, straight or crossed
- If both nodes are null, return true.
- If exactly one is null, or their values differ, return false.
- Try the straight pairing: match left with left and right with right. If both succeed, return true.
- Otherwise try the crossed pairing: match left with right and right with left, and return that result.
Step-by-step example
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]
- Roots (1, 1) match. Straight tries (2, 3) first, which fails on the values, so node 1 is flipped and the pairs become (2, 2) and (3, 3).
- At (2, 2): straight pairs (4, 4), which match as leaves, then (5, 5).
- At (5, 5): straight pairs (7, 8) and fails, so 5 is flipped and (7, 7) and (8, 8) match.
- At (3, 3): straight pairs (6, null) and fails, so 3 is flipped: (6, 6) matches and (null, null) matches.
- Every pair matched after flipping 1, 5 and 3, so the answer is true.
Code: Python, Java, C++
1class Solution:
2 def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
3 if not root1 and not root2:
4 return True
5 if not root1 or not root2 or root1.val != root2.val:
6 return False
7 straight = (self.flipEquiv(root1.left, root2.left) and
8 self.flipEquiv(root1.right, root2.right))
9 if straight:
10 return True
11 return (self.flipEquiv(root1.left, root2.right) and
12 self.flipEquiv(root1.right, root2.left))1class Solution {
2 public boolean flipEquiv(TreeNode root1, TreeNode root2) {
3 if (root1 == null && root2 == null) return true;
4 if (root1 == null || root2 == null || root1.val != root2.val) return false;
5 boolean straight = flipEquiv(root1.left, root2.left)
6 && flipEquiv(root1.right, root2.right);
7 if (straight) return true;
8 return flipEquiv(root1.left, root2.right)
9 && flipEquiv(root1.right, root2.left);
10 }
11}1class Solution {
2public:
3 bool flipEquiv(TreeNode* root1, TreeNode* root2) {
4 if (!root1 && !root2) return true;
5 if (!root1 || !root2 || root1->val != root2->val) return false;
6 bool straight = flipEquiv(root1->left, root2->left)
7 && flipEquiv(root1->right, root2->right);
8 if (straight) return true;
9 return flipEquiv(root1->left, root2->right)
10 && flipEquiv(root1->right, root2->left);
11 }
12};Time and space complexity
O(n)
With unique values, at most one of the two pairings gets past the first value check at each node, so each node is visited a constant number of times.
O(h)
The recursion goes as deep as the tree height h, which is n in the worst case of a chain.
Other approaches
Canonical form (time O(n), space O(h))
Rewrite each tree so that at every node the child with the smaller value (or a non-null child) goes on the left, then compare the two canonical trees with an ordinary same-tree check.
Choose the pairing by child values (time O(n), space O(h))
Instead of trying straight and then crossed, look at the left child values: if they match (or both are null) go straight, otherwise go crossed. One recursive call per child, no backtracking.
Common mistakes
- Returning false as soon as the straight pairing fails. A failed straight pairing is the signal to try the flip, not the answer.
- Forgetting the one-null case. If one node is null and the other is not, reading .val on the null one crashes.
- Assuming the flip must be applied to tree two. Flipping either side is the same thing, so one crossed comparison covers both.
Frequently asked questions
Is the straight-then-crossed recursion exponential?
Not with unique values. A wrong pairing compares two different values on its first step and returns false at once, so each node does a constant amount of extra work.
Do the flips have to be applied to the first tree?
No. Flipping a node in tree two has the same effect on the comparison as flipping its partner in tree one, so you only need to decide straight or crossed at each pair.
What if both trees are empty?
Two empty trees are equal with zero flips, so the answer is true. One empty tree and one non-empty tree can never match.