{"id":89,"date":"2018-12-06T22:14:39","date_gmt":"2018-12-06T22:14:39","guid":{"rendered":"https:\/\/andybase.com\/?p=89"},"modified":"2018-12-06T22:14:41","modified_gmt":"2018-12-06T22:14:41","slug":"951-flip-equivalent-binary-trees","status":"publish","type":"post","link":"https:\/\/andybase.com\/?p=89","title":{"rendered":"951 Flip Equivalent Binary Trees"},"content":{"rendered":"\n<p>Link:\u00a0<a href=\"https:\/\/leetcode.com\/problems\/flip-equivalent-binary-trees\/\">https:\/\/leetcode.com\/problems\/flip-equivalent-binary-trees\/\u00a0<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Thought<\/h2>\n\n\n\n<p>If 2 nodes are equivalent, either their left and right side match, or they are flipped match. So using a recursion(why recursion? It is a Tree!), and try to match each node pairs from root1 and root2.<\/p>\n\n\n\n<p>For each recursion level, I will first compare if current node pair have the same value. (I can return False immediately if not match). If it matches, there are 3 cases:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>They are identical -> recursion with (root1.left, root2.left,) and\u00a0 (root1.right, root2.right) should return True<\/li><li>They are flipped -> recursion with (root1.right, root2.left,) and\u00a0 (root1.left, root2.right) should return True<br><\/li><li>They are different -> Otherwise<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code brush: python; notranslate\"># Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.left = None\n#         self.right = None\n\nclass Solution:\n    def flipEquiv(self, root1, root2):\n        \"\"\"\n        :type root1: TreeNode\n        :type root2: TreeNode\n        :rtype: bool\n        \"\"\"\n        def compare(node1,node2):\n            if node1 == None and node2 == None:\n                return True\n            if node1 == None or node2 == None:\n                return False\n            if node1.val != node2.val:\n                return False\n            res = compare(node1.left, node2.left) and compare(node1.right, node2.right)\n            res = res or compare(node1.left, node2.right) and compare(node1.right, node2.left)\n            return res\n        return compare(root1, root2)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Runtime<\/h2>\n\n\n\n<p>Time: O(n)<\/p>\n\n\n\n<p>Space: O(1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Link:\u00a0https:\/\/leetcode.com\/problems\/flip-equivalent-binary-trees\/\u00a0 Thought If 2 nodes are equivalent, either their left and right side match, or they are flipped match. So [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[2],"tags":[4,3],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-contest","tag-leetcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paWPni-1r","_links":{"self":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":1,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}