We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9116bc6 commit f972620Copy full SHA for f972620
animation-simulation/二叉树/二叉树的前序遍历(栈).md
@@ -67,3 +67,32 @@ class Solution {
67
}
68
```
69
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
95
96
+}
97
+```
98
0 commit comments