Flip Binary Tree To Match Preorder Traversal - Problem

Imagine you have a binary tree where you can flip any node by swapping its left and right subtrees. Your goal is to transform the tree so that its preorder traversal matches a given sequence called voyage.

You're given:

  • A binary tree with n nodes, each uniquely valued from 1 to n
  • A sequence voyage representing the desired preorder traversal

Your task: Find the minimum number of flips needed to make the tree's preorder traversal match voyage. Return a list of all node values that need to be flipped.

Important: If it's impossible to achieve the desired traversal through flips, return [-1].

Example: If flipping node 1 swaps its left subtree (containing node 2) with its right subtree (containing node 3), the preorder changes from [1,2,...] to [1,3,...].

Input & Output

example_1.py โ€” Basic Flip Required
$ Input: root = [1,2,3], voyage = [1,3,2]
โ€บ Output: [1]
๐Ÿ’ก Note: We need to flip node 1 to swap its children. Originally preorder is [1,2,3], but we want [1,3,2]. Flipping node 1 swaps nodes 2 and 3, giving us the desired traversal.
example_2.py โ€” No Flips Needed
$ Input: root = [1,2,3], voyage = [1,2,3]
โ€บ Output: []
๐Ÿ’ก Note: The tree's natural preorder traversal already matches the voyage [1,2,3], so no flips are required. Return empty array.
example_3.py โ€” Impossible Case
$ Input: root = [1,2,3], voyage = [1,3,4]
โ€บ Output: [-1]
๐Ÿ’ก Note: The voyage contains node 4 which doesn't exist in our tree. No amount of flipping can create node 4, so it's impossible to match the voyage.

Constraints

  • The number of nodes in the tree is n
  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค Node.val โ‰ค n
  • All the values in the tree are unique
  • voyage.length == n
  • 1 โ‰ค voyage[i] โ‰ค n
  • All the values in voyage are unique

Visualization

Tap to expand
๐ŸŽญ Theater Director's Binary Tree ChallengeBefore: Wrong Order123Preorder: [1,2,3]Want: [1,3,2]After: Perfect Order132Preorder: [1,3,2] โœ“Flipped: [1]๐ŸŽฏ Director's Strategy1Check if lead actor is correct2If left understudy is wrong, swap!3Continue with next level actors4Record all swaps made๐Ÿ’ก Key InsightWe only try flipping ONCE per actor.If that doesn't work, the showcannot go on - return [-1]!Time: O(n) - visit each actor onceSpace: O(h) - stage height depthPerfect! Minimum swaps guaranteed
Understanding the Visualization
1
Check the Lead Actor
Start with the main actor (root). If they're not who we expect first in our sequence, the show can't go on!
2
Examine Understudies
Look at the left understudy. If they're not next in our desired sequence, swap them with the right understudy.
3
Continue the Chain
Move to the next level and repeat the process for each actor and their understudies.
4
Final Performance
If at any point the wrong actor appears and can't be fixed by swapping, declare it impossible. Otherwise, return the list of actors whose understudies were swapped.
Key Takeaway
๐ŸŽฏ Key Insight: This greedy approach works because each node can only be flipped once. If the left child doesn't match our expected sequence after considering a flip, then it's impossible to achieve the desired preorder traversal. The single-pass nature makes it both time and space efficient!
Asked in
Google 15 Amazon 8 Microsoft 6 Meta 4
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen