[LeetCode] 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Given two binary trees original and cloned and given a reference to a node target in the original tree.

The cloned tree is a copy of the original tree.

Return a reference to the same node in the cloned tree.

Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.

Follow up: Solve the problem if repeated values on the tree are allowed.

Example 1:

Input: tree = [7,4,3,null,null,6,19], target = 3
Output: 3
Explanation: 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.

Example 2:

Input: tree = [7], target =  7
Output: 7

Example 3:

Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
Output: 4

Example 4:

Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5
Output: 5

Example 5:

Input: tree = [1,2,null,3], target = 2
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [1, 10^4].
  • The values of the nodes of the tree are unique.
  • target node is a node from the original tree and is not null.

找出克隆二叉树中的相同节点。

给你两棵二叉树,原始树 original 和克隆树 cloned,以及一个位于原始树 original 中的目标节点 target。

其中,克隆树 cloned 是原始树 original 的一个 副本 。

请找出在树 cloned 中,与 target 相同 的节点,并返回对该节点的引用(在 C/C++ 等有指针的语言中返回 节点指针,其他语言返回节点本身)。

注意:

你 不能 对两棵二叉树,以及 target 节点进行更改。
只能 返回对克隆树 cloned 中已有的节点的引用。
进阶:如果树中允许出现值相同的节点,你将如何解答?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的重点在于followup,如果树中有相同节点如何比较,答案是只能通过比较节点的reference来判断,而不是比较节点值val。只要比对的是节点的reference,递归还是迭代做都是次要的了。我这里给出递归的代码供参考。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 class Solution {
12     public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
13         // corner case
14         if (original == null || original == cloned) {
15             return cloned;
16         }
17 
18         // normal case
19         TreeNode res = getTargetCopy(original.left, cloned.left, target);
20         if (res != null) {
21             return res;
22         }
23         return getTargetCopy(original.right, cloned.right, target);
24     }
25 }

JavaScript实现

 1 /**
 2  * Definition for a binary tree node.
 3  * function TreeNode(val) {
 4  *     this.val = val;
 5  *     this.left = this.right = null;
 6  * }
 7  */
 8 /**
 9  * @param {TreeNode} original
10  * @param {TreeNode} cloned
11  * @param {TreeNode} target
12  * @return {TreeNode}
13  */
14 
15 var getTargetCopy = function (original, cloned, target) {
16     // corner case
17     if (original === null || original === target) {
18         return cloned;
19     }
20 
21     // normal case
22     var res = getTargetCopy(original.left, cloned.left, target);
23     if (res !== null) {
24         return res;
25     }
26     return getTargetCopy(original.right, cloned.right, target);
27 };

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/14224696.html