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.
思路:就是通过target在original里面进行搜索,同时cloned同步进行移动,如果target找到了,那么cloned也就是对应的reference。
左边没有,就找右边;DFS求解;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
if(original == null || original == target) {
return cloned;
}
TreeNode res = getTargetCopy(original.left, cloned.left, target);
if(res != null) {
return res;
} else {
return getTargetCopy(original.right, cloned.right, target);
}
}
}