Find a Corresponding Node of a Binary Tree in a Clone of That Trees
July 27, 2020
Find a Corresponding Node of a Binary Tree in a Clone of That Tree
class Solution {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned,
final TreeNode target) {
return get(cloned, target.val);
}
public TreeNode get(TreeNode cloned, final int val) {
if (cloned == null) {
return null;
}
if (val == cloned.val) {
return cloned;
}
TreeNode leftVal = get(cloned.left, val);
TreeNode rightVal = get(cloned.right, val);
if (leftVal != null) {
return leftVal;
} else if (rightVal != null) {
return rightVal;
} else {
return null;
}
}
}
Find a Corresponding Node of a Binary Tree in a Clone of That Tree