{"id":6468,"date":"2020-03-13T22:58:43","date_gmt":"2020-03-14T05:58:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6468"},"modified":"2020-03-13T22:59:52","modified_gmt":"2020-03-14T05:59:52","slug":"leetcode-1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree"},"content":{"rendered":"\n<p>Given two binary trees&nbsp;<code>original<\/code>&nbsp;and&nbsp;<code>cloned<\/code>&nbsp;and given a reference to a node&nbsp;<code>target<\/code>&nbsp;in the original tree.<\/p>\n\n\n\n<p>The&nbsp;<code>cloned<\/code>&nbsp;tree is a&nbsp;<strong>copy of<\/strong>&nbsp;the&nbsp;<code>original<\/code>&nbsp;tree.<\/p>\n\n\n\n<p>Return&nbsp;<em>a reference to the same node<\/em>&nbsp;in the&nbsp;<code>cloned<\/code>&nbsp;tree.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that you are&nbsp;<strong>not allowed<\/strong>&nbsp;to change any of the two trees or the&nbsp;<code>target<\/code>&nbsp;node and the answer&nbsp;<strong>must be<\/strong>&nbsp;a reference to a node in the&nbsp;<code>cloned<\/code>&nbsp;tree.<\/p>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Solve the problem if repeated values on the tree are allowed.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/21\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tree = [7,4,3,null,null,6,19], target = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/21\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tree = [7], target =  7\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/21\/e3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/21\/e4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tree = [1,2,3,4,5,6,7,8,9,10], target = 5\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/21\/e5.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> tree = [1,2,null,3], target = 2\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the&nbsp;<code>tree<\/code>&nbsp;is in the range&nbsp;<code>[1, 10^4]<\/code>.<\/li><li>The values of the nodes of the&nbsp;<code>tree<\/code>&nbsp;are unique.<\/li><li><code>target<\/code>&nbsp;node is a&nbsp;node from the&nbsp;<code>original<\/code>&nbsp;tree and is not&nbsp;<code>null<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Traverse both trees in the same order, if original == target, return cloned.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {\n    if (!original) return nullptr;\n    if (original == target) return cloned;\n    auto l = getTargetCopy(original-&gt;left, cloned-&gt;left, target);\n    auto r = getTargetCopy(original-&gt;right, cloned-&gt;right, target);\n    return l ? l : r;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -&gt; TreeNode:\n    if not original: return None\n    if original == target: return cloned\n    return self.getTargetCopy(original.left, cloned.left, target) or \\\n           self.getTargetCopy(original.right, cloned.right, target)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given two binary trees&nbsp;original&nbsp;and&nbsp;cloned&nbsp;and given a reference to a node&nbsp;target&nbsp;in the original tree. The&nbsp;cloned&nbsp;tree is a&nbsp;copy of&nbsp;the&nbsp;original&nbsp;tree. Return&nbsp;a reference to the same node&nbsp;in the&nbsp;cloned&nbsp;tree. Note&nbsp;that&#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,17,28],"class_list":["post-6468","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6468","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=6468"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6468\/revisions"}],"predecessor-version":[{"id":6470,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6468\/revisions\/6470"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}