https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/

You are given the root of a binary tree with n nodes, where each node is uniquely assigned a value from 1 to n. You are also given a sequence of n values voyage, which is the desired pre-order traversal of the binary tree.

Any node in the binary tree can be flipped by swapping its left and right subtrees. For example, flipping node 1 will have the following effect:

Flip the smallest number of nodes so that the pre-order traversal of the tree matches voyage.

Return a list of the values of all flipped nodes. You may return the answer in any order. If it is impossible to flip the nodes in the tree to make the pre-order traversal match voyage, return the list [-1].

Example 1:

Input: root = [1,2], voyage = [2,1]
Output: [-1]
Explanation: It is impossible to flip the nodes such that the pre-order traversal matches voyage.

Example 2:

Input: root = [1,2,3], voyage = [1,3,2]
Output: [1]
Explanation: Flipping node 1 swaps nodes 2 and 3, so the pre-order traversal matches voyage.

Example 3:

Input: root = [1,2,3], voyage = [1,2,3]
Output: []
Explanation: The tree's pre-order traversal already matches voyage, so no nodes need to be flipped.

Constraints:

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

Efficient Solutions by OpHaxor:

List<Integer> res = new ArrayList<>();
int i = 0;
public List<Integer> flipMatchVoyage(TreeNode root, int[] v) {
    return dfs(root, v) ? res : Arrays.asList(-1);
}

public Boolean dfs(TreeNode node, int[] v) {
    if (node == null) return true;
    if (node.val != v[i++]) return false;
    if (node.left != null && node.left.val != v[i]) {
        res.add(node.val);
        return dfs(node.right, v) && dfs(node.left, v);
    }
    return dfs(node.left, v) && dfs(node.right, v);
}
void preorder(TreeNode* node, vector<int>& voyage, vector<int>& result, int& index, bool& canFlip)
    {
        if(node == nullptr || !canFlip)
        {
            return;
        }
        if(voyage[index] != node->val)
        {
            canFlip = false;
            return;
        }
        ++index;
        {
            if(node->left == nullptr && node->right == nullptr)
                return;
            auto leftValue = node->left != nullptr ? node->left->val : -1;
            auto rightValue = node->right != nullptr ? node->right->val : -1;
            if(leftValue != voyage[index] && rightValue != voyage[index])
            {
                canFlip = false;
                return;
            }
            else if(rightValue == voyage[index] && node->left != nullptr)
            {          
                auto temp = node->left;
                node->left = node->right;
                node->right = temp;
                result.push_back(node->val);
            }
        }
        preorder(node->left, voyage, result, index, canFlip);
        preorder(node->right, voyage, result, index, canFlip);
    }
    
    vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
        if(voyage.empty())
        {
            return {};
        }
        else if(voyage[0] != root->val)
        {
            return { -1 };
        }
        vector<int> result;
        auto index = 0;
        auto canFlip = true;
        preorder(root, voyage, result, index, canFlip);
        if(!canFlip)
        {
            result.clear();
            result.push_back(-1);
        }
        return result;
    }
def flipMatchVoyage(self, root, voyage):
    res = []
    stack = [root]
    i = 0
    while stack:
        node = stack.pop()
        if not node: continue
        if node and node.val != voyage[i]: return [-1]
        i += 1
        if node.right and node.right.val == voyage[i]:
            if node.left: res.append(node.val)
            stack.extend([node.left, node.right])
        else:
            stack.extend([node.right, node.left])
    return res

Leave a reply