Problem
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:
1 | Input: tree = [7,4,3,null,null,6,19], target = 3 |
Example 2:
1 | Input: tree = [7], target = 7 |
Example 3:
1 | Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4 |
Example 4:
1 | Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5 |
Example 5:
1 | Input: tree = [1,2,null,3], target = 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 theoriginal
tree and is notnull
.
Analysis
这是一道考察点比较新的二叉树题目,但实际上是非常简单的。题目给出两颗完全一样的二叉树,还有一个指向original
某个节点的指针,要求在cloned
上面找出指向相同节点的指针。
如果只是AC这道题目是非常简单的,因为题目给出了限制二叉树中的值都是唯一的,所以我们可以通过值来判断即可。遍历二叉树,找到和target
值一样的节点就是要找的节点了。
但是题目也给了Follow up,如果树里面存在相同值的节点,这道题目又该怎么解呢?可以利用两颗二叉树是一样的这个特点,同时对两颗二叉树进行遍历,通过比对target
和original
中节点的地址去判断是否找到该节点,找到后,返回当时指向cloned
的那个节点即可。
Solution
无
Code
1 | /** |
Summary
这道题目是比较简单的二叉树题目,在值都是唯一的情况下,题目是比较好解决的;当值不能成为判断的标准,就需要用到地址了。这道题这道题目的分享到这里,谢谢您的支持!