mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 12:56:17 +00:00
添加 Swift 代码实现
This commit is contained in:
parent
ef44b3f3be
commit
7572d7c2f7
@ -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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user