添加 Swift 代码实现

This commit is contained in:
frank-tian 2021-07-19 23:39:19 +08:00
parent ef44b3f3be
commit 7572d7c2f7

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