添加 Swift 代码实现

pull/34/head
frank-tian 2021-07-19 23:22:39 +08:00
parent 9116bc63ce
commit f972620873
1 changed files with 29 additions and 0 deletions

View File

@ -67,3 +67,32 @@ class Solution {
}
```
Swift Code
```swift
class Solution {
func preorderTraversal(_ root: TreeNode?) -> [Int] {
var list:[Int] = []
var stack:[TreeNode] = []
guard root != nil else {
return list
}
stack.append(root!)
while !stack.isEmpty {
let temp = stack.popLast()
if let right = temp?.right {
stack.append(right)
}
if let left = temp?.left {
stack.append(left)
}
//这里也可以放到前面
list.append((temp?.val)!)
}
return list
}
}
```