Class Solution
java.lang.Object
g0901_1000.s0971_flip_binary_tree_to_match_preorder_traversal.Solution
971 - Flip Binary Tree To Match Preorder Traversal.<p>Medium</p>
<p>You are given the <code>root</code> of a binary tree with <code>n</code> nodes, where each node is uniquely assigned a value from <code>1</code> to <code>n</code>. You are also given a sequence of <code>n</code> values <code>voyage</code>, which is the <strong>desired</strong> <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order" target="_top"><strong>pre-order traversal</strong></a> of the binary tree.</p>
<p>Any node in the binary tree can be <strong>flipped</strong> by swapping its left and right subtrees. For example, flipping node 1 will have the following effect:</p>
<p><img src="https://assets.leetcode.com/uploads/2021/02/15/fliptree.jpg" alt="" /></p>
<p>Flip the <strong>smallest</strong> number of nodes so that the <strong>pre-order traversal</strong> of the tree <strong>matches</strong> <code>voyage</code>.</p>
<p>Return <em>a list of the values of all <strong>flipped</strong> nodes. You may return the answer in <strong>any order</strong>. If it is <strong>impossible</strong> to flip the nodes in the tree to make the pre-order traversal match</em> <code>voyage</code><em>, return the list</em> <code>[-1]</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2019/01/02/1219-01.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,2], voyage = [2,1]</p>
<p><strong>Output:</strong> [-1]</p>
<p><strong>Explanation:</strong> It is impossible to flip the nodes such that the pre-order traversal matches voyage.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2019/01/02/1219-02.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,2,3], voyage = [1,3,2]</p>
<p><strong>Output:</strong> [1]</p>
<p><strong>Explanation:</strong> Flipping node 1 swaps nodes 2 and 3, so the pre-order traversal matches voyage.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2019/01/02/1219-02.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,2,3], voyage = [1,2,3]</p>
<p><strong>Output:</strong> []</p>
<p><strong>Explanation:</strong> The tree’s pre-order traversal already matches voyage, so no nodes need to be flipped.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>n == voyage.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= Node.val, voyage[i] <= n</code></li>
<li>All the values in the tree are <strong>unique</strong>.</li>
<li>All the values in <code>voyage</code> are <strong>unique</strong>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
flipMatchVoyage
-