Skip to content

Commit 4f3afc1

Browse files
committed
feat: add solutions to lc problem: No.0623
No.0623.Add One Row to Tree
1 parent 3ebdabd commit 4f3afc1

File tree

3 files changed

+392
-15
lines changed

3 files changed

+392
-15
lines changed

solution/0600-0699/0623.Add One Row to Tree/README.md

+195-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:DFS**
58+
59+
**方法二:BFS**
60+
5761
<!-- tabs:start -->
5862

5963
### **Python3**
@@ -68,24 +72,50 @@
6872
# self.left = left
6973
# self.right = right
7074
class Solution:
71-
def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode:
75+
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
7276
def dfs(root, d):
7377
if root is None:
7478
return
7579
if d == depth - 1:
76-
l = TreeNode(val=val, left=root.left)
77-
r = TreeNode(val=val, right=root.right)
78-
root.left, root.right = l, r
80+
root.left = TreeNode(val, root.left, None)
81+
root.right = TreeNode(val, None, root.right)
7982
return
8083
dfs(root.left, d + 1)
8184
dfs(root.right, d + 1)
8285

8386
if depth == 1:
84-
return TreeNode(val=val, left=root)
87+
return TreeNode(val, root)
8588
dfs(root, 1)
8689
return root
8790
```
8891

92+
```python
93+
# Definition for a binary tree node.
94+
# class TreeNode:
95+
# def __init__(self, val=0, left=None, right=None):
96+
# self.val = val
97+
# self.left = left
98+
# self.right = right
99+
class Solution:
100+
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
101+
if depth == 1:
102+
return TreeNode(val, root)
103+
q = deque([root])
104+
i = 0
105+
while q:
106+
i += 1
107+
for _ in range(len(q)):
108+
node = q.popleft()
109+
if node.left:
110+
q.append(node.left)
111+
if node.right:
112+
q.append(node.right)
113+
if i == depth - 1:
114+
node.left = TreeNode(val, node.left, None)
115+
node.right = TreeNode(val, None, node.right)
116+
return root
117+
```
118+
89119
### **Java**
90120

91121
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -137,6 +167,51 @@ class Solution {
137167
}
138168
```
139169

170+
```java
171+
/**
172+
* Definition for a binary tree node.
173+
* public class TreeNode {
174+
* int val;
175+
* TreeNode left;
176+
* TreeNode right;
177+
* TreeNode() {}
178+
* TreeNode(int val) { this.val = val; }
179+
* TreeNode(int val, TreeNode left, TreeNode right) {
180+
* this.val = val;
181+
* this.left = left;
182+
* this.right = right;
183+
* }
184+
* }
185+
*/
186+
class Solution {
187+
public TreeNode addOneRow(TreeNode root, int val, int depth) {
188+
if (depth == 1) {
189+
return new TreeNode(val, root, null);
190+
}
191+
Deque<TreeNode> q = new ArrayDeque<>();
192+
q.offer(root);
193+
int i = 0;
194+
while (!q.isEmpty()) {
195+
++i;
196+
for (int k = q.size(); k > 0; --k) {
197+
TreeNode node = q.pollFirst();
198+
if (node.left != null) {
199+
q.offer(node.left);
200+
}
201+
if (node.right != null) {
202+
q.offer(node.right);
203+
}
204+
if (i == depth - 1) {
205+
node.left = new TreeNode(val, node.left, null);
206+
node.right = new TreeNode(val, null, node.right);
207+
}
208+
}
209+
}
210+
return root;
211+
}
212+
}
213+
```
214+
140215
### **C++**
141216

142217
```cpp
@@ -180,6 +255,45 @@ public:
180255
};
181256
```
182257
258+
```cpp
259+
/**
260+
* Definition for a binary tree node.
261+
* struct TreeNode {
262+
* int val;
263+
* TreeNode *left;
264+
* TreeNode *right;
265+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
266+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
267+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
268+
* };
269+
*/
270+
class Solution {
271+
public:
272+
TreeNode* addOneRow(TreeNode* root, int val, int depth) {
273+
if (depth == 1) return new TreeNode(val, root, nullptr);
274+
queue<TreeNode*> q{{root}};
275+
int i = 0;
276+
while (!q.empty())
277+
{
278+
++i;
279+
for (int k = q.size(); k; --k)
280+
{
281+
TreeNode* node = q.front();
282+
q.pop();
283+
if (node->left) q.push(node->left);
284+
if (node->right) q.push(node->right);
285+
if (i == depth - 1)
286+
{
287+
node->left = new TreeNode(val, node->left, nullptr);
288+
node->right = new TreeNode(val, nullptr, node->right);
289+
}
290+
}
291+
}
292+
return root;
293+
}
294+
};
295+
```
296+
183297
### **Go**
184298

185299
```go
@@ -213,8 +327,84 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
213327
}
214328
```
215329

330+
```go
331+
/**
332+
* Definition for a binary tree node.
333+
* type TreeNode struct {
334+
* Val int
335+
* Left *TreeNode
336+
* Right *TreeNode
337+
* }
338+
*/
339+
func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
340+
if depth == 1 {
341+
return &TreeNode{val, root, nil}
342+
}
343+
q := []*TreeNode{root}
344+
i := 0
345+
for len(q) > 0 {
346+
i++
347+
for k := len(q); k > 0; k-- {
348+
node := q[0]
349+
q = q[1:]
350+
if node.Left != nil {
351+
q = append(q, node.Left)
352+
}
353+
if node.Right != nil {
354+
q = append(q, node.Right)
355+
}
356+
if i == depth-1 {
357+
node.Left = &TreeNode{val, node.Left, nil}
358+
node.Right = &TreeNode{val, nil, node.Right}
359+
}
360+
}
361+
}
362+
return root
363+
}
364+
```
365+
216366
### **TypeScript**
217367

368+
```ts
369+
/**
370+
* Definition for a binary tree node.
371+
* class TreeNode {
372+
* val: number
373+
* left: TreeNode | null
374+
* right: TreeNode | null
375+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
376+
* this.val = (val===undefined ? 0 : val)
377+
* this.left = (left===undefined ? null : left)
378+
* this.right = (right===undefined ? null : right)
379+
* }
380+
* }
381+
*/
382+
383+
function addOneRow(
384+
root: TreeNode | null,
385+
val: number,
386+
depth: number,
387+
): TreeNode | null {
388+
function dfs(root, d) {
389+
if (!root) {
390+
return;
391+
}
392+
if (d == depth - 1) {
393+
root.left = new TreeNode(val, root.left, null);
394+
root.right = new TreeNode(val, null, root.right);
395+
return;
396+
}
397+
dfs(root.left, d + 1);
398+
dfs(root.right, d + 1);
399+
}
400+
if (depth == 1) {
401+
return new TreeNode(val, root);
402+
}
403+
dfs(root, 1);
404+
return root;
405+
}
406+
```
407+
218408
```ts
219409
/**
220410
* Definition for a binary tree node.

0 commit comments

Comments
 (0)