{"id":3315,"date":"2018-07-26T21:18:30","date_gmt":"2018-07-27T04:18:30","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3315"},"modified":"2018-12-15T01:18:03","modified_gmt":"2018-12-15T09:18:03","slug":"leetcode-101-symmetric-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-101-symmetric-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 101. Symmetric Tree"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).<\/p>\n<p>For example, this binary tree\u00a0<code>[1,2,2,3,4,4,3]<\/code>\u00a0is symmetric:<\/p>\n<pre class=\"crayon:false\">    1\r\n   \/ \\\r\n  2   2\r\n \/ \\ \/ \\\r\n3  4 4  3\r\n<\/pre>\n<p>But the following\u00a0<code>[1,2,2,null,3,null,3]<\/code>\u00a0is not:<\/p>\n<pre class=\"crayon:false \">    1\r\n   \/ \\\r\n  2   2\r\n   \\   \\\r\n   3    3\r\n<\/pre>\n<p><b>Note:<\/b><br \/>\nBonus points if you could solve it both recursively and iteratively.<\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  bool isSymmetric(TreeNode* root) {\r\n    return isMirror(root, root);\r\n  }\r\nprivate:\r\n  bool isMirror(TreeNode* root1, TreeNode* root2) {\r\n    if (!root1 &amp;&amp; !root2) return true;\r\n    if (!root1 || !root2) return false;\r\n    return root1-&gt;val == root2-&gt;val \r\n           &amp;&amp; isMirror(root1-&gt;right, root2-&gt;left) \r\n           &amp;&amp; isMirror(root1-&gt;left, root2-&gt;right);\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 48 ms\r\n\"\"\"\r\nclass Solution:\r\n  def isSymmetric(self, root):\r\n    def isMirror(root1, root2):\r\n      if not root1 and not root2: return True\r\n      if not root1 or not root2: return False\r\n      return root1.val == root2.val \\\r\n        and isMirror(root1.left, root2.right) \\\r\n        and isMirror(root2.left, root1.right)\r\n    return isMirror(root, root)<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-100-same-tree\/\">\u82b1\u82b1\u9171 Leetcode 100. Same Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree\u00a0[1,2,2,3,4,4,3]\u00a0is symmetric: 1&#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":[222,349,17,350,28],"class_list":["post-3315","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-mirror","tag-recursion","tag-symmetric","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3315","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=3315"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3315\/revisions"}],"predecessor-version":[{"id":4437,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3315\/revisions\/4437"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}