-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindNodeInClonedTree.cpp
More file actions
48 lines (45 loc) · 1.13 KB
/
Copy pathFindNodeInClonedTree.cpp
File metadata and controls
48 lines (45 loc) · 1.13 KB
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
#include <bits/stdc++.h>
using namespace std;
// Find a Corresponding Node of a Binary Tree in a Clone of That Tree
/**
* 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(NULL), right(NULL) {}
};
class Solution
{
public:
TreeNode *getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target)
{
TreeNode *found;
queue<TreeNode *> q1, q2;
TreeNode *root1 = original, *root2 = cloned;
q1.push(root1);
q2.push(root2);
while (!q1.empty())
{
root1 = q1.front();
root2 = q2.front();
q1.pop();
q2.pop();
if (root1 == target)
return root2;
if (root1->left)
q1.push(root1->left), q2.push(root2->left);
if (root1->right)
q2.push(root2->right), q1.push(root1->right);
}
return NULL;
}
};