Skip to content

Commit 6ee7e26

Browse files
committed
feat: add solutions to lc problem: No.1379
1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree
1 parent b209088 commit 6ee7e26

File tree

5 files changed

+257
-25
lines changed

5 files changed

+257
-25
lines changed

solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
<li><code>target</code>&nbsp;节点是树&nbsp;<code>original</code>&nbsp;中的一个节点,并且不会是&nbsp;<code>null</code>&nbsp;。</li>
8181
</ul>
8282

83-
8483
## 解法
8584

8685
<!-- 这里可写通用的实现逻辑 -->
@@ -92,15 +91,101 @@
9291
<!-- 这里可写当前语言的特殊实现逻辑 -->
9392

9493
```python
95-
94+
# Definition for a binary tree node.
95+
# class TreeNode:
96+
# def __init__(self, x):
97+
# self.val = x
98+
# self.left = None
99+
# self.right = None
100+
101+
class Solution:
102+
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
103+
res = None
104+
105+
def dfs(original, cloned):
106+
nonlocal res
107+
if cloned is None:
108+
return
109+
if original == target:
110+
res = cloned
111+
return
112+
dfs(original.left, cloned.left)
113+
dfs(original.right, cloned.right)
114+
115+
dfs(original, cloned)
116+
return res
96117
```
97118

98119
### **Java**
99120

100121
<!-- 这里可写当前语言的特殊实现逻辑 -->
101122

102123
```java
124+
/**
125+
* Definition for a binary tree node.
126+
* public class TreeNode {
127+
* int val;
128+
* TreeNode left;
129+
* TreeNode right;
130+
* TreeNode(int x) { val = x; }
131+
* }
132+
*/
133+
134+
class Solution {
135+
private TreeNode res;
136+
137+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
138+
dfs(original, cloned, target);
139+
return res;
140+
}
141+
142+
private void dfs(TreeNode original, TreeNode cloned, TreeNode target) {
143+
if (cloned == null) {
144+
return;
145+
}
146+
if (original == target) {
147+
res = cloned;
148+
return;
149+
}
150+
dfs(original.left, cloned.left, target);
151+
dfs(original.right, cloned.right, target);
152+
}
153+
}
154+
```
103155

156+
### **C++**
157+
158+
```cpp
159+
/**
160+
* Definition for a binary tree node.
161+
* struct TreeNode {
162+
* int val;
163+
* TreeNode *left;
164+
* TreeNode *right;
165+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
166+
* };
167+
*/
168+
169+
class Solution {
170+
public:
171+
TreeNode* res;
172+
173+
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
174+
dfs(original, cloned, target);
175+
return res;
176+
}
177+
178+
void dfs(TreeNode* original, TreeNode* cloned, TreeNode* target) {
179+
if (!cloned) return;
180+
if (original == target)
181+
{
182+
res = cloned;
183+
return;
184+
}
185+
dfs(original->left, cloned->left, target);
186+
dfs(original->right, cloned->right, target);
187+
}
188+
};
104189
```
105190
106191
### **...**

solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/README_EN.md

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66

77
<p>Given two binary trees <code>original</code> and <code>cloned</code> and given a reference to a node <code>target</code> in the original tree.</p>
88

9-
10-
119
<p>The <code>cloned</code> tree is a <strong>copy of</strong> the <code>original</code> tree.</p>
1210

13-
14-
1511
<p>Return <em>a reference to the same node</em> in the <code>cloned</code> tree.</p>
1612

17-
18-
1913
<p><strong>Note</strong> that you are <strong>not allowed</strong> to change any of the two trees or the <code>target</code> node and the answer <strong>must be</strong> a reference to a node in the <code>cloned</code> tree.</p>
2014

21-
22-
2315
<p><strong>Follow up:</strong>&nbsp;Solve the problem if repeated values on the tree are allowed.</p>
2416

25-
26-
2717
<p>&nbsp;</p>
2818

2919
<p><strong>Example 1:</strong></p>
@@ -40,8 +30,6 @@
4030

4131
</pre>
4232

43-
44-
4533
<p><strong>Example 2:</strong></p>
4634

4735
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1379.Find%20a%20Corresponding%20Node%20of%20a%20Binary%20Tree%20in%20a%20Clone%20of%20That%20Tree/images/e2.png" style="width: 221px; height: 159px;" />
@@ -54,8 +42,6 @@
5442

5543
</pre>
5644

57-
58-
5945
<p><strong>Example 3:</strong></p>
6046

6147
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1379.Find%20a%20Corresponding%20Node%20of%20a%20Binary%20Tree%20in%20a%20Clone%20of%20That%20Tree/images/e3.png" style="width: 459px; height: 486px;" />
@@ -68,8 +54,6 @@
6854

6955
</pre>
7056

71-
72-
7357
<p><strong>Example 4:</strong></p>
7458

7559
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1379.Find%20a%20Corresponding%20Node%20of%20a%20Binary%20Tree%20in%20a%20Clone%20of%20That%20Tree/images/e4.png" style="width: 555px; height: 239px;" />
@@ -82,8 +66,6 @@
8266

8367
</pre>
8468

85-
86-
8769
<p><strong>Example 5:</strong></p>
8870

8971
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1379.Find%20a%20Corresponding%20Node%20of%20a%20Binary%20Tree%20in%20a%20Clone%20of%20That%20Tree/images/e5.png" style="width: 427px; height: 345px;" />
@@ -96,14 +78,10 @@
9678

9779
</pre>
9880

99-
100-
10181
<p>&nbsp;</p>
10282

10383
<p><strong>Constraints:</strong></p>
10484

105-
106-
10785
<ul>
10886
<li>The number of nodes in the <code>tree</code> is in the range <code>[1, 10^4]</code>.</li>
10987
<li>The values of the nodes of the <code>tree</code> are unique.</li>
@@ -117,13 +95,99 @@
11795
### **Python3**
11896

11997
```python
120-
98+
# Definition for a binary tree node.
99+
# class TreeNode:
100+
# def __init__(self, x):
101+
# self.val = x
102+
# self.left = None
103+
# self.right = None
104+
105+
class Solution:
106+
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
107+
res = None
108+
109+
def dfs(original, cloned):
110+
nonlocal res
111+
if cloned is None:
112+
return
113+
if original == target:
114+
res = cloned
115+
return
116+
dfs(original.left, cloned.left)
117+
dfs(original.right, cloned.right)
118+
119+
dfs(original, cloned)
120+
return res
121121
```
122122

123123
### **Java**
124124

125125
```java
126+
/**
127+
* Definition for a binary tree node.
128+
* public class TreeNode {
129+
* int val;
130+
* TreeNode left;
131+
* TreeNode right;
132+
* TreeNode(int x) { val = x; }
133+
* }
134+
*/
135+
136+
class Solution {
137+
private TreeNode res;
138+
139+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
140+
dfs(original, cloned, target);
141+
return res;
142+
}
143+
144+
private void dfs(TreeNode original, TreeNode cloned, TreeNode target) {
145+
if (cloned == null) {
146+
return;
147+
}
148+
if (original == target) {
149+
res = cloned;
150+
return;
151+
}
152+
dfs(original.left, cloned.left, target);
153+
dfs(original.right, cloned.right, target);
154+
}
155+
}
156+
```
126157

158+
### **C++**
159+
160+
```cpp
161+
/**
162+
* Definition for a binary tree node.
163+
* struct TreeNode {
164+
* int val;
165+
* TreeNode *left;
166+
* TreeNode *right;
167+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
168+
* };
169+
*/
170+
171+
class Solution {
172+
public:
173+
TreeNode* res;
174+
175+
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
176+
dfs(original, cloned, target);
177+
return res;
178+
}
179+
180+
void dfs(TreeNode* original, TreeNode* cloned, TreeNode* target) {
181+
if (!cloned) return;
182+
if (original == target)
183+
{
184+
res = cloned;
185+
return;
186+
}
187+
dfs(original->left, cloned->left, target);
188+
dfs(original->right, cloned->right, target);
189+
}
190+
};
127191
```
128192
129193
### **...**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
class Solution {
12+
public:
13+
TreeNode* res;
14+
15+
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
16+
dfs(original, cloned, target);
17+
return res;
18+
}
19+
20+
void dfs(TreeNode* original, TreeNode* cloned, TreeNode* target) {
21+
if (!cloned) return;
22+
if (original == target)
23+
{
24+
res = cloned;
25+
return;
26+
}
27+
dfs(original->left, cloned->left, target);
28+
dfs(original->right, cloned->right, target);
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
class Solution {
12+
private TreeNode res;
13+
14+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
15+
dfs(original, cloned, target);
16+
return res;
17+
}
18+
19+
private void dfs(TreeNode original, TreeNode cloned, TreeNode target) {
20+
if (cloned == null) {
21+
return;
22+
}
23+
if (original == target) {
24+
res = cloned;
25+
return;
26+
}
27+
dfs(original.left, cloned.left, target);
28+
dfs(original.right, cloned.right, target);
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
10+
res = None
11+
12+
def dfs(original, cloned):
13+
nonlocal res
14+
if cloned is None:
15+
return
16+
if original == target:
17+
res = cloned
18+
return
19+
dfs(original.left, cloned.left)
20+
dfs(original.right, cloned.right)
21+
22+
dfs(original, cloned)
23+
return res

0 commit comments

Comments
 (0)