添加 Swift 代码实现

This commit is contained in:
frank-tian 2021-07-19 23:22:39 +08:00
parent 9116bc63ce
commit f972620873

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
}
}
```