-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path623.swift
64 lines (59 loc) · 1.57 KB
/
623.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// 623.swift
// swift-leetcode
//
// Created by Q YiZhong on 2019/5/10.
// Copyright © 2019 YiZhong Qi. All rights reserved.
//
import Foundation
/**
Review Level: ⭐️
难点:
需要插入的第d行的上一行左右子树都必须有新的值为v的node
*/
func addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? {
guard root != nil else {
return root
}
if d == 1 {
let newRoot = TreeNode.init(v)
newRoot.left = root
return newRoot
}
var queue: [TreeNode] = []
queue.append(root!)
var i = 1
while !queue.isEmpty {
if i + 1 == d {
while !queue.isEmpty {
let node = queue.removeFirst()
if node.left != nil {
let newNode = TreeNode.init(v)
newNode.left = node.left
node.left = newNode
} else {
node.left = TreeNode.init(v)
}
if node.right != nil {
let newNode = TreeNode.init(v)
newNode.right = node.right
node.right = newNode
} else {
node.right = TreeNode.init(v)
}
}
return root
}
for _ in 0..<queue.count {
let node = queue.removeFirst()
if node.left != nil {
queue.append(node.left!)
}
if node.right != nil {
queue.append(node.right!)
}
}
i += 1
}
return root
}