Tap here to switch tabs
Problem
Submissions

Problem: Flip Binary Tree To Match Preorder Traversal

Try to solve the Flip Binary Tree To Match Preorder Traversal problem.

Statement

You are given the root of a binary tree with nn nodes, where each node has a unique value from 11 to nn. You are also given an integer array voyage representing a target preorder traversal of the binary tree.

Flipping a node means swapping its left and right child subtrees.

Return a list of values of all nodes that were flipped to make the preorder traversal of the tree match voyage. If it is impossible to make the preorder traversal match voyage, return [−1][-1]. If there are multiple answers, return any of them.

Constraints:

  • The number of nodes in the tree is nn.

  • n==n == voyage.length

  • 1≤n≤1001 \leq n \leq 100

  • 1≤1 \leq Node.val, voyage[i] ≤n\leq n

  • All the values in the tree are unique.

  • All the values in voyage are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Flip Binary Tree To Match Preorder Traversal

Try to solve the Flip Binary Tree To Match Preorder Traversal problem.

Statement

You are given the root of a binary tree with nn nodes, where each node has a unique value from 11 to nn. You are also given an integer array voyage representing a target preorder traversal of the binary tree.

Flipping a node means swapping its left and right child subtrees.

Return a list of values of all nodes that were flipped to make the preorder traversal of the tree match voyage. If it is impossible to make the preorder traversal match voyage, return [−1][-1]. If there are multiple answers, return any of them.

Constraints:

  • The number of nodes in the tree is nn.

  • n==n == voyage.length

  • 1≤n≤1001 \leq n \leq 100

  • 1≤1 \leq Node.val, voyage[i] ≤n\leq n

  • All the values in the tree are unique.

  • All the values in voyage are unique.