{"id":5337,"date":"2019-07-22T22:17:03","date_gmt":"2019-07-23T05:17:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5337"},"modified":"2019-07-22T22:17:51","modified_gmt":"2019-07-23T05:17:51","slug":"leetcode-226-invert-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-226-invert-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 226. Invert Binary Tree"},"content":{"rendered":"\n<p>Invert a binary tree.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Input:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">     4\n   \/   \\\n  2     7\n \/ \\   \/ \\\n1   3 6   9<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">     4\n   \/   \\\n  7     2\n \/ \\   \/ \\\n9   6 3   1<\/pre>\n\n\n\n<p><strong>Trivia:<\/strong><br>This problem was inspired by&nbsp;<a href=\"https:\/\/twitter.com\/mxcl\/status\/608682016205344768\" target=\"_blank\" rel=\"noreferrer noopener\">this original tweet<\/a>&nbsp;by&nbsp;<a href=\"https:\/\/twitter.com\/mxcl\" target=\"_blank\" rel=\"noreferrer noopener\">Max Howell<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Google: 90% of our engineers use the software you wrote (Homebrew), but you can\u2019t invert a binary tree on a whiteboard so f*** off.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Recursive invert the left and right subtrees and swap them.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  TreeNode* invertTree(TreeNode* root) {\n    if (!root) return nullptr;\n    TreeNode* tmp = root->right;\n    root->right = invertTree(root->left); \n    root->left = invertTree(tmp);\n    return root;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def invertTree(self, root: TreeNode) -> TreeNode:\n    if not root: return None\n    root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)\n    return root\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Invert a binary tree. Example: Input: 4 \/ \\ 2 7 \/ \\ \/ \\ 1 3 6 9 Output: 4 \/ \\ 7 2&#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,1],"tags":[222,17,28],"class_list":["post-5337","post","type-post","status-publish","format-standard","hentry","category-tree","category-uncategorized","tag-easy","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5337","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=5337"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5337\/revisions"}],"predecessor-version":[{"id":5339,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5337\/revisions\/5339"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}