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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
return dfs(original, cloned, target);
}

private TreeNode dfs(TreeNode original, TreeNode cloned, TreeNode target) {
if (original == target) {
return cloned;
}

if (original.left != null) {
TreeNode result = dfs(original.left, cloned.left, target);
if (result != null) {
return result;
}
}
if (original.right != null) {
TreeNode result = dfs(original.right, cloned.right, target);
if (result != null) {
return result;
}
}

return null;
}
}

References

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