File tree 2 files changed +119
-0
lines changed
lcof2/剑指 Offer II 043. 往完全二叉树添加节点
2 files changed +119
-0
lines changed Original file line number Diff line number Diff line change @@ -420,6 +420,68 @@ CBTInserter.prototype.get_root = function () {
420
420
*/
421
421
```
422
422
423
+ #### Swift
424
+
425
+ ``` swift
426
+ /* public class TreeNode {
427
+ * public var val: Int
428
+ * public var left: TreeNode?
429
+ * public var right: TreeNode?
430
+ * public init() { self.val = 0; self.left = nil; self.right = nil; }
431
+ * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
432
+ * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
433
+ * self.val = val
434
+ * self.left = left
435
+ * self.right = right
436
+ * }
437
+ * }
438
+ */
439
+
440
+ class CBTInserter {
441
+ private var tree: [TreeNode] = []
442
+
443
+ init (_ root : TreeNode? ) {
444
+ guard let root = root else { return }
445
+ var q: [TreeNode] = [root]
446
+ while ! q.isEmpty {
447
+ for _ in 0 ..< q.count {
448
+ let node = q.removeFirst ()
449
+ tree.append (node)
450
+ if let left = node.left {
451
+ q.append (left)
452
+ }
453
+ if let right = node.right {
454
+ q.append (right)
455
+ }
456
+ }
457
+ }
458
+ }
459
+
460
+ func insert (_ val : Int ) -> Int {
461
+ let p = tree[(tree.count - 1 ) / 2 ]
462
+ let node = TreeNode (val)
463
+ tree.append (node)
464
+ if p.left == nil {
465
+ p.left = node
466
+ } else {
467
+ p.right = node
468
+ }
469
+ return p.val
470
+ }
471
+
472
+ func get_root () -> TreeNode? {
473
+ return tree.isEmpty ? nil : tree[0 ]
474
+ }
475
+ }
476
+
477
+ /**
478
+ * Your CBTInserter object will be instantiated and called as such:
479
+ * let obj = CBTInserter(root)
480
+ * let param_1 = obj.insert(val)
481
+ * let param_2 = obj.get_root()
482
+ */
483
+ ```
484
+
423
485
<!-- tabs:end -->
424
486
425
487
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /* public class TreeNode {
2
+ * public var val: Int
3
+ * public var left: TreeNode?
4
+ * public var right: TreeNode?
5
+ * public init() { self.val = 0; self.left = nil; self.right = nil; }
6
+ * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
7
+ * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
8
+ * self.val = val
9
+ * self.left = left
10
+ * self.right = right
11
+ * }
12
+ * }
13
+ */
14
+
15
+ class CBTInserter {
16
+ private var tree : [ TreeNode ] = [ ]
17
+
18
+ init ( _ root: TreeNode ? ) {
19
+ guard let root = root else { return }
20
+ var q : [ TreeNode ] = [ root]
21
+ while !q. isEmpty {
22
+ for _ in 0 ..< q. count {
23
+ let node = q. removeFirst ( )
24
+ tree. append ( node)
25
+ if let left = node. left {
26
+ q. append ( left)
27
+ }
28
+ if let right = node. right {
29
+ q. append ( right)
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ func insert( _ val: Int ) -> Int {
36
+ let p = tree [ ( tree. count - 1 ) / 2 ]
37
+ let node = TreeNode ( val)
38
+ tree. append ( node)
39
+ if p. left == nil {
40
+ p. left = node
41
+ } else {
42
+ p. right = node
43
+ }
44
+ return p. val
45
+ }
46
+
47
+ func get_root( ) -> TreeNode ? {
48
+ return tree. isEmpty ? nil : tree [ 0 ]
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Your CBTInserter object will be instantiated and called as such:
54
+ * let obj = CBTInserter(root)
55
+ * let param_1 = obj.insert(val)
56
+ * let param_2 = obj.get_root()
57
+ */
You can’t perform that action at this time.
0 commit comments