-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1379_find_target.cpp
More file actions
30 lines (26 loc) · 840 Bytes
/
1379_find_target.cpp
File metadata and controls
30 lines (26 loc) · 840 Bytes
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
27
28
29
30
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
if (original == nullptr || cloned == nullptr) return nullptr;
if (cloned->val == target->val) return cloned;
TreeNode* l = getTargetCopy(original->left, cloned->left, target);
TreeNode* r = getTargetCopy(original->right, cloned->right, target);
if (l == nullptr) return r;
else return l;
}
};