Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0623
Browse files Browse the repository at this point in the history
No.0623.Add One Row to Tree
  • Loading branch information
yanglbme committed Aug 5, 2022
1 parent 3ebdabd commit 4f3afc1
Show file tree
Hide file tree
Showing 3 changed files with 392 additions and 15 deletions.
200 changes: 195 additions & 5 deletions solution/0600-0699/0623.Add One Row to Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@

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

**方法一:DFS**

**方法二:BFS**

<!-- tabs:start -->

### **Python3**
Expand All @@ -68,24 +72,50 @@
# self.left = left
# self.right = right
class Solution:
def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
def dfs(root, d):
if root is None:
return
if d == depth - 1:
l = TreeNode(val=val, left=root.left)
r = TreeNode(val=val, right=root.right)
root.left, root.right = l, r
root.left = TreeNode(val, root.left, None)
root.right = TreeNode(val, None, root.right)
return
dfs(root.left, d + 1)
dfs(root.right, d + 1)

if depth == 1:
return TreeNode(val=val, left=root)
return TreeNode(val, root)
dfs(root, 1)
return root
```

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
return TreeNode(val, root)
q = deque([root])
i = 0
while q:
i += 1
for _ in range(len(q)):
node = q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if i == depth - 1:
node.left = TreeNode(val, node.left, None)
node.right = TreeNode(val, None, node.right)
return root
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
return new TreeNode(val, root, null);
}
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
int i = 0;
while (!q.isEmpty()) {
++i;
for (int k = q.size(); k > 0; --k) {
TreeNode node = q.pollFirst();
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
if (i == depth - 1) {
node.left = new TreeNode(val, node.left, null);
node.right = new TreeNode(val, null, node.right);
}
}
}
return root;
}
}
```

### **C++**

```cpp
Expand Down Expand Up @@ -180,6 +255,45 @@ public:
};
```
```cpp
/**
* Definition for a binary tree node.
* 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:
TreeNode* addOneRow(TreeNode* root, int val, int depth) {
if (depth == 1) return new TreeNode(val, root, nullptr);
queue<TreeNode*> q{{root}};
int i = 0;
while (!q.empty())
{
++i;
for (int k = q.size(); k; --k)
{
TreeNode* node = q.front();
q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
if (i == depth - 1)
{
node->left = new TreeNode(val, node->left, nullptr);
node->right = new TreeNode(val, nullptr, node->right);
}
}
}
return root;
}
};
```

### **Go**

```go
Expand Down Expand Up @@ -213,8 +327,84 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
}
```

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
if depth == 1 {
return &TreeNode{val, root, nil}
}
q := []*TreeNode{root}
i := 0
for len(q) > 0 {
i++
for k := len(q); k > 0; k-- {
node := q[0]
q = q[1:]
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
if i == depth-1 {
node.Left = &TreeNode{val, node.Left, nil}
node.Right = &TreeNode{val, nil, node.Right}
}
}
}
return root
}
```

### **TypeScript**

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function addOneRow(
root: TreeNode | null,
val: number,
depth: number,
): TreeNode | null {
function dfs(root, d) {
if (!root) {
return;
}
if (d == depth - 1) {
root.left = new TreeNode(val, root.left, null);
root.right = new TreeNode(val, null, root.right);
return;
}
dfs(root.left, d + 1);
dfs(root.right, d + 1);
}
if (depth == 1) {
return new TreeNode(val, root);
}
dfs(root, 1);
return root;
}
```

```ts
/**
* Definition for a binary tree node.
Expand Down
Loading

0 comments on commit 4f3afc1

Please sign in to comment.