Skip to content

Commit 4c5a06e

Browse files
committed
feat: add solutions to lcp problem: No.67
1 parent 105b453 commit 4c5a06e

File tree

5 files changed

+244
-1
lines changed

5 files changed

+244
-1
lines changed

lcp/LCP 67. 装饰树/README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,155 @@
4040

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

43+
**方法一:递归**
44+
45+
我们设计一个函数 $dfs(root)$,表示将灯饰插入以 $root$ 为根节点的树中,返回插入灯饰后的树的根节点。那么答案就是 $dfs(root)$。
46+
47+
函数 $dfs(root)$ 的逻辑如下:
48+
49+
- 若 $root$ 为空,则返回空;
50+
- 否则,递归地对 $root$ 的左右子树分别调用 $dfs$ 函数,得到插入灯饰后的左右子树的根节点 $l$ 和 $r$;
51+
- 若 $l$ 不为空,则我们创建一个新节点 $TreeNode(-1, l, null)$,并将其作为 $root$ 的左子节点;
52+
- 若 $r$ 不为空,则我们创建一个新节点 $TreeNode(-1, null, r)$,并将其作为 $root$ 的右子节点;
53+
54+
最后,返回 $root$。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树的节点数。
57+
4358
<!-- tabs:start -->
4459

4560
### **Python3**
4661

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

4964
```python
50-
65+
# Definition for a binary tree node.
66+
# class TreeNode:
67+
# def __init__(self, val=0, left=None, right=None):
68+
# self.val = val
69+
# self.left = left
70+
# self.right = right
71+
class Solution:
72+
def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
73+
def dfs(root):
74+
if root is None:
75+
return None
76+
l, r = dfs(root.left), dfs(root.right)
77+
if l:
78+
root.left = TreeNode(-1, l)
79+
if r:
80+
root.right = TreeNode(-1, None, r)
81+
return root
82+
83+
return dfs(root)
5184
```
5285

5386
### **Java**
5487

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

5790
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
public TreeNode expandBinaryTree(TreeNode root) {
108+
return dfs(root);
109+
}
110+
111+
private TreeNode dfs(TreeNode root) {
112+
if (root == null) {
113+
return null;
114+
}
115+
TreeNode l = dfs(root.left);
116+
TreeNode r = dfs(root.right);
117+
if (l != null) {
118+
root.left = new TreeNode(-1, l, null);
119+
}
120+
if (r != null) {
121+
root.right = new TreeNode(-1, null, r);
122+
}
123+
return root;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
/**
132+
* Definition for a binary tree node.
133+
* struct TreeNode {
134+
* int val;
135+
* TreeNode *left;
136+
* TreeNode *right;
137+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
138+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
TreeNode* expandBinaryTree(TreeNode* root) {
145+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
146+
if (!root) {
147+
return nullptr;
148+
}
149+
TreeNode* l = dfs(root->left);
150+
TreeNode* r = dfs(root->right);
151+
if (l) {
152+
root->left = new TreeNode(-1, l, nullptr);
153+
}
154+
if (r) {
155+
root->right = new TreeNode(-1, nullptr, r);
156+
}
157+
return root;
158+
};
159+
return dfs(root);
160+
}
161+
};
162+
```
58163
164+
### **Go**
165+
166+
```go
167+
/**
168+
* Definition for a binary tree node.
169+
* type TreeNode struct {
170+
* Val int
171+
* Left *TreeNode
172+
* Right *TreeNode
173+
* }
174+
*/
175+
func expandBinaryTree(root *TreeNode) *TreeNode {
176+
var dfs func(*TreeNode) *TreeNode
177+
dfs = func(root *TreeNode) *TreeNode {
178+
if root == nil {
179+
return root
180+
}
181+
l, r := dfs(root.Left), dfs(root.Right)
182+
if l != nil {
183+
root.Left = &TreeNode{-1, l, nil}
184+
}
185+
if r != nil {
186+
root.Right = &TreeNode{-1, nil, r}
187+
}
188+
return root
189+
}
190+
return dfs(root)
191+
}
59192
```
60193

61194
### **...**

lcp/LCP 67. 装饰树/Solution.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* expandBinaryTree(TreeNode* root) {
15+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
16+
if (!root) {
17+
return nullptr;
18+
}
19+
TreeNode* l = dfs(root->left);
20+
TreeNode* r = dfs(root->right);
21+
if (l) {
22+
root->left = new TreeNode(-1, l, nullptr);
23+
}
24+
if (r) {
25+
root->right = new TreeNode(-1, nullptr, r);
26+
}
27+
return root;
28+
};
29+
return dfs(root);
30+
}
31+
};

lcp/LCP 67. 装饰树/Solution.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func expandBinaryTree(root *TreeNode) *TreeNode {
10+
var dfs func(*TreeNode) *TreeNode
11+
dfs = func(root *TreeNode) *TreeNode {
12+
if root == nil {
13+
return root
14+
}
15+
l, r := dfs(root.Left), dfs(root.Right)
16+
if l != nil {
17+
root.Left = &TreeNode{-1, l, nil}
18+
}
19+
if r != nil {
20+
root.Right = &TreeNode{-1, nil, r}
21+
}
22+
return root
23+
}
24+
return dfs(root)
25+
}

lcp/LCP 67. 装饰树/Solution.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode expandBinaryTree(TreeNode root) {
18+
return dfs(root);
19+
}
20+
21+
private TreeNode dfs(TreeNode root) {
22+
if (root == null) {
23+
return null;
24+
}
25+
TreeNode l = dfs(root.left);
26+
TreeNode r = dfs(root.right);
27+
if (l != null) {
28+
root.left = new TreeNode(-1, l, null);
29+
}
30+
if (r != null) {
31+
root.right = new TreeNode(-1, null, r);
32+
}
33+
return root;
34+
}
35+
}

lcp/LCP 67. 装饰树/Solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9+
def dfs(root):
10+
if root is None:
11+
return None
12+
l, r = dfs(root.left), dfs(root.right)
13+
if l:
14+
root.left = TreeNode(-1, l)
15+
if r:
16+
root.right = TreeNode(-1, None, r)
17+
return root
18+
19+
return dfs(root)

0 commit comments

Comments
 (0)