-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1379.找出克隆二叉树中的相同节点.cpp
47 lines (42 loc) · 1.15 KB
/
1379.找出克隆二叉树中的相同节点.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
/*
* @lc app=leetcode.cn id=1379 lang=cpp
*
* [1379] 找出克隆二叉树中的相同节点
*/
// @lc code=start
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode(int x) : val(x), left(NULL), right(NULL) {}
// };
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
// cout << target->val << endl;
TreeNode* node = preOrder(cloned, target->val);
// if(node){
// cout << node->val << endl;
// cout << node << endl;
// return node;
// }
return node ? node : nullptr;
}
TreeNode* preOrder(TreeNode *root, int target){
if(root == nullptr){
return nullptr;
}
// cout << root->val << endl;
if(root->val == target ){
return root;
}
TreeNode* node = nullptr;
node = preOrder(root->left, target);
if(node != nullptr){
return node;
}
node = preOrder(root->right, target);
return node;
}
};
// @lc code=end