Same Up to Swapping Children
You are given the roots first and second of two binary trees. Values may repeat.
One move picks a node and swaps its left and right child, together with everything below them. A node with one child can be picked too: the child changes sides.
Return True if some number of moves turns first into second, and False otherwise.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
Swap the children of the root, then the children of the node 2.
Example 2
In the first tree the node 4 hangs under 2. In the second it hangs under 3.
Example 3
Swapping the children of the root moves the only child to the other side.
Constraints
1 <= number of nodes <= 40in each tree0 <= node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Trees problems
- Tree From an OutlineHard
- Two Subtrees ApartHard
- Two Swapped ValuesHard
- Binary Tree Inorder TraversalBeginner
- Binary Tree Postorder TraversalBeginner
- Binary Tree Preorder TraversalBeginner
- Collect the LeavesBeginner
- Count the LeavesBeginner