{"id":3604,"date":"2018-08-17T23:44:25","date_gmt":"2018-08-18T06:44:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3604"},"modified":"2018-08-19T19:28:42","modified_gmt":"2018-08-20T02:28:42","slug":"leetcode-617-merge-two-binary-trees","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-617-merge-two-binary-trees\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 617. Merge Two Binary Trees"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 617. Merge Two Binary Trees \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5cEP16\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/EmVsf2sMNiU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.<\/p>\n<p>You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> \r\n\tTree 1                     Tree 2                  \r\n          1                         2                             \r\n         \/ \\                       \/ \\                            \r\n        3   2                     1   3                        \r\n       \/                           \\   \\                      \r\n      5                             4   7                  \r\n<b>Output:<\/b> \r\nMerged tree:\r\n\t     3\r\n\t    \/ \\\r\n\t   4   5\r\n\t  \/ \\   \\ \r\n\t 5   4   7\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The merging process must start from the root nodes of both trees.<\/p>\n<p><ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2404451723245401\"\n     data-ad-slot=\"7983117522\"><br \/>\n<\/ins><\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Reuse t1\/t2<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 52 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {\r\n    if (t1 == nullptr) return t2;\r\n    if (t2 == nullptr) return t1;\r\n\r\n    auto root = t1;\r\n    root-&gt;val += t2-&gt;val;\r\n    root-&gt;left = mergeTrees(t1-&gt;left, t2-&gt;left);\r\n    root-&gt;right = mergeTrees(t1-&gt;right, t2-&gt;right);\r\n\r\n    return root;\r\n  }\r\n};<\/pre>\n<p>Create a copy<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 52 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {\r\n    if (t1 == nullptr) return copyTree(t2);\r\n    if (t2 == nullptr) return copyTree(t1);\r\n\r\n    TreeNode* root = new TreeNode(t1-&gt;val + t2-&gt;val);    \r\n    root-&gt;left = mergeTrees(t1-&gt;left, t2-&gt;left);\r\n    root-&gt;right = mergeTrees(t1-&gt;right, t2-&gt;right);\r\n\r\n    return root;\r\n  }\r\nprivate:\r\n  TreeNode* copyTree(TreeNode* root) {\r\n    if (root == nullptr) return nullptr;\r\n    TreeNode* r = new TreeNode(root-&gt;val);\r\n    r-&gt;left = copyTree(root-&gt;left);\r\n    r-&gt;right = copyTree(root-&gt;right);\r\n    return r;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are&#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":[177,129,17,28],"class_list":["post-3604","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-merge","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3604","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=3604"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3604\/revisions"}],"predecessor-version":[{"id":3633,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3604\/revisions\/3633"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}