Search
⌘K

Leetcode 971. Flip Binary Tree To Match Preorder Traversal

Given a binary tree and a target preorder sequence voyage, find the minimum set of nodes to flip (swap their left and right children) so the tree's preorder equals voyage, or return [-1] if impossible. Algorithmically, simulate a preorder traversal and greedily flip a node when its left child's value doesn't match the next voyage value, recording flipped nodes.


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Comments

Your account is free and you can post anonymously if you choose.