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

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

解法一: 前序遍历

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 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) {
return null;
} else if (original == target) {
return cloned;
}
TreeNode ans = this.getTargetCopy(original.left, cloned.left, target);
if (ans != null) {
return ans;
}
return this.getTargetCopy(original.right, cloned.right, target);
}
}
作者

wuhunyu

发布于

2024-04-03

更新于

2025-01-15

许可协议