添加 Swift 代码实现

pull/34/head
frank-tian 2021-07-19 23:39:19 +08:00
parent ef44b3f3be
commit 7572d7c2f7
1 changed files with 27 additions and 0 deletions

View File

@ -51,5 +51,32 @@ class Solution {
}
```
Swift Code
```swift
class Solution {
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var arr:[Int] = []
var cur = root
var stack:[TreeNode] = []
while !stack.isEmpty || cur != nil {
//找到当前应该遍历的那个节点
while cur != nil {
stack.append(cur!)
cur = cur!.left
}
//此时指针指向空,也就是没有左子节点,则开始执行出栈操作
if let temp = stack.popLast() {
arr.append(temp.val)
//指向右子节点
cur = temp.right
}
}
return arr
}
}
```
###