-
Notifications
You must be signed in to change notification settings - Fork 0
/
1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree.cpp
71 lines (51 loc) · 1.59 KB
/
1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<vector>
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void depthFirstSearch(TreeNode* original, TreeNode* cloned){
if(original->val == cloned->val && original->val == targetValue){
targetNode = cloned;
return;
}
if(original->left){
depthFirstSearch(original->left,cloned->left);
}
if(original->right){
depthFirstSearch(original->right,cloned->right);
}
}
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
targetValue = target->val;
depthFirstSearch(original,cloned);
return targetNode;
}
private:
int targetValue;
TreeNode* targetNode;
};
int main(){
Solution solution;
TreeNode* root1 = new TreeNode(7);
root1->left = new TreeNode(4);
TreeNode* p1 = new TreeNode(3);
root1->right = p1;
p1->left = new TreeNode(6);
p1->right = new TreeNode(19);
TreeNode* root2 = new TreeNode(7);
root2->left = new TreeNode(4);
TreeNode* q1 = new TreeNode(3);
root2->right = q1;
q1->left = new TreeNode(6);
q1->right = new TreeNode(19);
solution.getTargetCopy(root1,root2,q1);
return 0;
}