{"id":129,"date":"2017-08-27T21:28:31","date_gmt":"2017-08-27T13:28:31","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=129"},"modified":"2018-11-27T16:34:19","modified_gmt":"2018-11-27T08:34:19","slug":"101-symmetric-tree","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=129","title":{"rendered":"101. Symmetric Tree"},"content":{"rendered":"<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>    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>    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<p>&nbsp;<\/p>\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(object):\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(object):\r\n    def isSymmetric(self, root):\r\n        &quot;&quot;&quot;\r\n        :type root: TreeNode\r\n        :rtype: bool\r\n        &quot;&quot;&quot;\r\n        return self.isMirror(root, root)\r\n\r\n    def isMirror(self, t1, t2):\r\n        if t1 is None and t2 is None:\r\n            return True\r\n        elif t1 is None or t2 is None:\r\n            return False\r\n        return t1.val == t2.val and self.isMirror(t1.right, t2.left) and self.isMirror(t1.left, t2.right)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, check whether it is a mirror of it<a href=\"https:\/\/www.ccagml.com\/?p=129\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">101. Symmetric 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\/129"}],"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=129"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}