Skip to content

Commit f972620

Browse files
committed
添加 Swift 代码实现
1 parent 9116bc6 commit f972620

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

animation-simulation/二叉树/二叉树的前序遍历(栈).md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,32 @@ class Solution {
6767
}
6868
```
6969

70+
Swift Code:
71+
72+
```swift
73+
class Solution {
74+
75+
func preorderTraversal(_ root: TreeNode?) -> [Int] {
76+
var list:[Int] = []
77+
var stack:[TreeNode] = []
78+
79+
guard root != nil else {
80+
return list
81+
}
82+
stack.append(root!)
83+
while !stack.isEmpty {
84+
let temp = stack.popLast()
85+
if let right = temp?.right {
86+
stack.append(right)
87+
}
88+
if let left = temp?.left {
89+
stack.append(left)
90+
}
91+
//这里也可以放到前面
92+
list.append((temp?.val)!)
93+
}
94+
return list
95+
}
96+
}
97+
```
98+

0 commit comments

Comments
 (0)