{"id":184,"date":"2018-05-24T17:18:53","date_gmt":"2018-05-24T09:18:53","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=184"},"modified":"2018-11-27T17:22:29","modified_gmt":"2018-11-27T09:22:29","slug":"226-invert-binary-tree","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=184","title":{"rendered":"226. Invert Binary Tree"},"content":{"rendered":"<p>Invert a binary tree.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Input:<\/p>\n<pre>     4\r\n   \/   \\\r\n  2     7\r\n \/ \\   \/ \\\r\n1   3 6   9<\/pre>\n<p>Output:<\/p>\n<pre>     4\r\n   \/   \\\r\n  7     2\r\n \/ \\   \/ \\\r\n9   6 3   1<\/pre>\n<p><strong>Trivia:<\/strong><br \/>\nThis problem was inspired by\u00a0<a href=\"https:\/\/twitter.com\/mxcl\/status\/608682016205344768\" target=\"_blank\" rel=\"noopener\">this original tweet<\/a>\u00a0by\u00a0<a href=\"https:\/\/twitter.com\/mxcl\" target=\"_blank\" rel=\"noopener\">Max Howell<\/a>:<\/p>\n<blockquote><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<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n# Definition for a binary tree node.\r\n# class TreeNode:\r\n#     def __init__(self, x):\r\n#         self.val = x\r\n#         self.left = None\r\n#         self.right = None\r\n\r\nclass Solution:\r\n    def invertTree(self, root):\r\n        &quot;&quot;&quot;\r\n        :type root: TreeNode\r\n        :rtype: TreeNode\r\n        &quot;&quot;&quot;\r\n        if root:\r\n            if root.val:\r\n                root.left, root.right = root.right, root.left\r\n                if root.left:\r\n                    self.invertTree(root.left)\r\n                if root.right:\r\n                    self.invertTree(root.right)\r\n        return root\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Invert a binary tree. Example: Input: 4 \/ \\ 2 7 \/ \\ \/ \\<a href=\"https:\/\/www.ccagml.com\/?p=184\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">226. Invert Binary Tree<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/184"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}