{"id":4611,"date":"2019-01-05T20:53:57","date_gmt":"2019-01-06T04:53:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4611"},"modified":"2019-01-05T21:06:10","modified_gmt":"2019-01-06T05:06:10","slug":"leetcode-971-flip-binary-tree-to-match-preorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-971-flip-binary-tree-to-match-preorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 971. Flip Binary Tree To Match Preorder Traversal"},"content":{"rendered":"\n<p>Given a binary tree with&nbsp;<code>N<\/code>&nbsp;nodes, each node has a different value from&nbsp;<code>{1, ..., N}<\/code>.<\/p>\n\n\n\n<p>A node in this binary tree can be&nbsp;<em>flipped<\/em>&nbsp;by swapping the left child and the right child of that node.<\/p>\n\n\n\n<p>Consider the sequence of&nbsp;<code>N<\/code>&nbsp;values reported by a preorder traversal starting from the root.&nbsp; Call such a sequence of&nbsp;<code>N<\/code>&nbsp;values the&nbsp;<em>voyage<\/em>&nbsp;of the tree.<\/p>\n\n\n\n<p>(Recall that a&nbsp;<em>preorder traversal<\/em>&nbsp;of a node means we report the current node&#8217;s value, then preorder-traverse the left child, then preorder-traverse the right child.)<\/p>\n\n\n\n<p>Our goal is to flip the&nbsp;<strong>least number<\/strong>&nbsp;of nodes in the tree so that the voyage of the tree matches the&nbsp;<code>voyage<\/code>we are given.<\/p>\n\n\n\n<p>If we can do so, then return a&nbsp;list&nbsp;of the values of all nodes flipped.&nbsp; You may return the answer in any order.<\/p>\n\n\n\n<p>If we cannot do so, then return the list&nbsp;<code>[-1]<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/02\/1219-01.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>root = [1,2], voyage = [2,1]\n<strong>Output: <\/strong>[-1]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/02\/1219-02.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>root = [1,2,3], voyage = [1,3,2]\n<strong>Output: <\/strong>[1]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/02\/1219-02.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>root = [1,2,3], voyage = [1,2,3]\n<strong>Output: <\/strong>[]\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= N &lt;= 100<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Pre-order traversal<\/h2>\n\n\n\n<p>if root-&gt;val != v[pos] return [-1]<br>if root-&gt;left?-&gt;val != v[pos + 1], swap the nodes<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">c++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {\n    vector<int> flips;\n    int pos = 0;\n    solve(root, voyage, pos, flips);\n    return flips;\n  }\nprivate:\n  void solve(TreeNode* root, const vector<int>& voyage, int& pos, vector<int>& flips) {\n    if (!root) return;\n    if (root->val != voyage.at(pos)) {\n      flips.clear();\n      flips.push_back(-1);\n      return;\n    }\n    if (root->left && root->left->val != voyage.at(pos + 1)) {\n      swap(root->left, root->right);\n      flips.push_back(root->val);\n    }\n    ++pos;\n    solve(root->left, voyage, pos, flips);\n    solve(root->right, voyage, pos, flips);   \n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua, 60 ms\nclass Solution:\n  def flipMatchVoyage(self, root, voyage):\n    self.pos = 0\n    self.flips = []\n    def solve(root):\n      if not root: return      \n      if root.val != voyage[self.pos]:\n        self.flips = [-1]\n        return\n      if root.left and root.left.val != voyage[self.pos + 1]:\n        root.left, root.right = root.right, root.left\n        self.flips.append(root.val)\n      self.pos += 1\n      solve(root.left)\n      solve(root.right)    \n    solve(root)\n    return self.flips\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree with&nbsp;N&nbsp;nodes, each node has a different value from&nbsp;{1, &#8230;, N}. A node in this binary tree can be&nbsp;flipped&nbsp;by swapping the left&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[177,290,32,28],"class_list":["post-4611","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-swap","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4611"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4611\/revisions"}],"predecessor-version":[{"id":4614,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4611\/revisions\/4614"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}