Skip to content

Commit ca637fa

Browse files
committed
feat: add solutions to lc/lcof2 problems
* lc No.0919 & lcof2 No.043.Complete Binary Tree Inserter
1 parent a7e034d commit ca637fa

File tree

15 files changed

+536
-370
lines changed

15 files changed

+536
-370
lines changed

lcof2/剑指 Offer II 043. 往完全二叉树添加节点/README.md

+141-65
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@
4949

5050
## 解法
5151

52-
### 方法一
52+
### 方法一:BFS
53+
54+
我们可以使用一个数组 $tree$ 来存储完全二叉树的所有节点。在初始化时,我们使用一个队列 $q$ 来层序遍历给定的树,并将所有节点存储到数组 $tree$ 中。
55+
56+
在插入节点时,我们可以通过数组 $tree$ 来找到新节点的父节点 $p$。然后我们创建一个新节点 $node$,将其插入到数组 $tree$ 中,并将 $node$ 作为 $p$ 的左子节点或右子节点。最后返回 $p$ 的值。
57+
58+
在获取根节点时,我们直接返回数组 $tree$ 的第一个元素。
59+
60+
时间复杂度方面,初始化时需要 $O(n)$ 的时间,插入节点和获取根节点的时间复杂度均为 $O(1)$。空间复杂度为 $O(n)$。其中 $n$ 为树中节点的数量。
5361

5462
<!-- tabs:start -->
5563

@@ -61,7 +69,8 @@
6169
# self.left = left
6270
# self.right = right
6371
class CBTInserter:
64-
def __init__(self, root: TreeNode):
72+
73+
def __init__(self, root: Optional[TreeNode]):
6574
self.tree = []
6675
q = deque([root])
6776
while q:
@@ -73,24 +82,23 @@ class CBTInserter:
7382
if node.right:
7483
q.append(node.right)
7584

76-
def insert(self, v: int) -> int:
77-
pid = (len(self.tree) - 1) >> 1
78-
node = TreeNode(v)
85+
def insert(self, val: int) -> int:
86+
p = self.tree[(len(self.tree) - 1) // 2]
87+
node = TreeNode(val)
7988
self.tree.append(node)
80-
p = self.tree[pid]
8189
if p.left is None:
8290
p.left = node
8391
else:
8492
p.right = node
8593
return p.val
8694

87-
def get_root(self) -> TreeNode:
95+
def get_root(self) -> Optional[TreeNode]:
8896
return self.tree[0]
8997

9098

9199
# Your CBTInserter object will be instantiated and called as such:
92100
# obj = CBTInserter(root)
93-
# param_1 = obj.insert(v)
101+
# param_1 = obj.insert(val)
94102
# param_2 = obj.get_root()
95103
```
96104

@@ -111,29 +119,29 @@ class CBTInserter:
111119
* }
112120
*/
113121
class CBTInserter {
114-
private List<TreeNode> tree;
122+
private List<TreeNode> tree = new ArrayList<>();
115123

116124
public CBTInserter(TreeNode root) {
117-
tree = new ArrayList<>();
118125
Deque<TreeNode> q = new ArrayDeque<>();
119126
q.offer(root);
120127
while (!q.isEmpty()) {
121-
TreeNode node = q.pollFirst();
122-
tree.add(node);
123-
if (node.left != null) {
124-
q.offer(node.left);
125-
}
126-
if (node.right != null) {
127-
q.offer(node.right);
128+
for (int i = q.size(); i > 0; --i) {
129+
TreeNode node = q.poll();
130+
tree.add(node);
131+
if (node.left != null) {
132+
q.offer(node.left);
133+
}
134+
if (node.right != null) {
135+
q.offer(node.right);
136+
}
128137
}
129138
}
130139
}
131140

132-
public int insert(int v) {
133-
int pid = (tree.size() - 1) >> 1;
134-
TreeNode node = new TreeNode(v);
141+
public int insert(int val) {
142+
TreeNode p = tree.get((tree.size() - 1) / 2);
143+
TreeNode node = new TreeNode(val);
135144
tree.add(node);
136-
TreeNode p = tree.get(pid);
137145
if (p.left == null) {
138146
p.left = node;
139147
} else {
@@ -150,7 +158,7 @@ class CBTInserter {
150158
/**
151159
* Your CBTInserter object will be instantiated and called as such:
152160
* CBTInserter obj = new CBTInserter(root);
153-
* int param_1 = obj.insert(v);
161+
* int param_1 = obj.insert(val);
154162
* TreeNode param_2 = obj.get_root();
155163
*/
156164
```
@@ -169,40 +177,47 @@ class CBTInserter {
169177
*/
170178
class CBTInserter {
171179
public:
172-
vector<TreeNode*> tree;
173-
174180
CBTInserter(TreeNode* root) {
175181
queue<TreeNode*> q{{root}};
176-
while (!q.empty()) {
177-
auto node = q.front();
178-
q.pop();
179-
tree.push_back(node);
180-
if (node->left) q.push(node->left);
181-
if (node->right) q.push(node->right);
182+
while (q.size()) {
183+
for (int i = q.size(); i; --i) {
184+
auto node = q.front();
185+
q.pop();
186+
tree.push_back(node);
187+
if (node->left) {
188+
q.push(node->left);
189+
}
190+
if (node->right) {
191+
q.push(node->right);
192+
}
193+
}
182194
}
183195
}
184196

185-
int insert(int v) {
186-
int pid = tree.size() - 1 >> 1;
187-
TreeNode* node = new TreeNode(v);
197+
int insert(int val) {
198+
auto p = tree[(tree.size() - 1) / 2];
199+
auto node = new TreeNode(val);
188200
tree.push_back(node);
189-
TreeNode* p = tree[pid];
190-
if (!p->left)
201+
if (!p->left) {
191202
p->left = node;
192-
else
203+
} else {
193204
p->right = node;
205+
}
194206
return p->val;
195207
}
196208

197209
TreeNode* get_root() {
198210
return tree[0];
199211
}
212+
213+
private:
214+
vector<TreeNode*> tree;
200215
};
201216

202217
/**
203218
* Your CBTInserter object will be instantiated and called as such:
204219
* CBTInserter* obj = new CBTInserter(root);
205-
* int param_1 = obj->insert(v);
220+
* int param_1 = obj->insert(val);
206221
* TreeNode* param_2 = obj->get_root();
207222
*/
208223
```
@@ -224,24 +239,25 @@ func Constructor(root *TreeNode) CBTInserter {
224239
q := []*TreeNode{root}
225240
tree := []*TreeNode{}
226241
for len(q) > 0 {
227-
node := q[0]
228-
tree = append(tree, node)
229-
q = q[1:]
230-
if node.Left != nil {
231-
q = append(q, node.Left)
232-
}
233-
if node.Right != nil {
234-
q = append(q, node.Right)
242+
for i := len(q); i > 0; i-- {
243+
node := q[0]
244+
q = q[1:]
245+
tree = append(tree, node)
246+
if node.Left != nil {
247+
q = append(q, node.Left)
248+
}
249+
if node.Right != nil {
250+
q = append(q, node.Right)
251+
}
235252
}
236253
}
237254
return CBTInserter{tree}
238255
}
239256

240-
func (this *CBTInserter) Insert(v int) int {
241-
pid := (len(this.tree) - 1) >> 1
242-
node := &TreeNode{Val: v}
257+
func (this *CBTInserter) Insert(val int) int {
258+
p := this.tree[(len(this.tree)-1)/2]
259+
node := &TreeNode{val, nil, nil}
243260
this.tree = append(this.tree, node)
244-
p := this.tree[pid]
245261
if p.Left == nil {
246262
p.Left = node
247263
} else {
@@ -257,11 +273,70 @@ func (this *CBTInserter) Get_root() *TreeNode {
257273
/**
258274
* Your CBTInserter object will be instantiated and called as such:
259275
* obj := Constructor(root);
260-
* param_1 := obj.Insert(v);
276+
* param_1 := obj.Insert(val);
261277
* param_2 := obj.Get_root();
262278
*/
263279
```
264280

281+
```ts
282+
/**
283+
* Definition for a binary tree node.
284+
* class TreeNode {
285+
* val: number
286+
* left: TreeNode | null
287+
* right: TreeNode | null
288+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
289+
* this.val = (val===undefined ? 0 : val)
290+
* this.left = (left===undefined ? null : left)
291+
* this.right = (right===undefined ? null : right)
292+
* }
293+
* }
294+
*/
295+
296+
class CBTInserter {
297+
private tree: TreeNode[] = [];
298+
299+
constructor(root: TreeNode | null) {
300+
if (root === null) {
301+
return;
302+
}
303+
const q: TreeNode[] = [root];
304+
while (q.length > 0) {
305+
const t: TreeNode[] = [];
306+
for (const node of q) {
307+
this.tree.push(node);
308+
node.left !== null && t.push(node.left);
309+
node.right !== null && t.push(node.right);
310+
}
311+
q.splice(0, q.length, ...t);
312+
}
313+
}
314+
315+
insert(val: number): number {
316+
const p = this.tree[(this.tree.length - 1) >> 1];
317+
const node = new TreeNode(val);
318+
this.tree.push(node);
319+
if (p.left === null) {
320+
p.left = node;
321+
} else {
322+
p.right = node;
323+
}
324+
return p.val;
325+
}
326+
327+
get_root(): TreeNode | null {
328+
return this.tree[0];
329+
}
330+
}
331+
332+
/**
333+
* Your CBTInserter object will be instantiated and called as such:
334+
* var obj = new CBTInserter(root)
335+
* var param_1 = obj.insert(val)
336+
* var param_2 = obj.get_root()
337+
*/
338+
```
339+
265340
```js
266341
/**
267342
* Definition for a binary tree node.
@@ -276,29 +351,30 @@ func (this *CBTInserter) Get_root() *TreeNode {
276351
*/
277352
var CBTInserter = function (root) {
278353
this.tree = [];
354+
if (root === null) {
355+
return;
356+
}
279357
const q = [root];
280-
while (q.length) {
281-
const node = q.shift();
282-
this.tree.push(node);
283-
if (node.left) {
284-
q.push(node.left);
285-
}
286-
if (node.right) {
287-
q.push(node.right);
358+
while (q.length > 0) {
359+
const t = [];
360+
for (const node of q) {
361+
this.tree.push(node);
362+
node.left !== null && t.push(node.left);
363+
node.right !== null && t.push(node.right);
288364
}
365+
q.splice(0, q.length, ...t);
289366
}
290367
};
291368

292369
/**
293-
* @param {number} v
370+
* @param {number} val
294371
* @return {number}
295372
*/
296-
CBTInserter.prototype.insert = function (v) {
297-
const pid = (this.tree.length - 1) >> 1;
298-
const node = new TreeNode(v);
373+
CBTInserter.prototype.insert = function (val) {
374+
const p = this.tree[(this.tree.length - 1) >> 1];
375+
const node = new TreeNode(val);
299376
this.tree.push(node);
300-
const p = this.tree[pid];
301-
if (!p.left) {
377+
if (p.left === null) {
302378
p.left = node;
303379
} else {
304380
p.right = node;
@@ -316,7 +392,7 @@ CBTInserter.prototype.get_root = function () {
316392
/**
317393
* Your CBTInserter object will be instantiated and called as such:
318394
* var obj = new CBTInserter(root)
319-
* var param_1 = obj.insert(v)
395+
* var param_1 = obj.insert(val)
320396
* var param_2 = obj.get_root()
321397
*/
322398
```

0 commit comments

Comments
 (0)