Skip to content

Commit

Permalink
二叉树的迭代遍历:补充Swift实现
Browse files Browse the repository at this point in the history
# Conflicts:
#	problems/二叉树的迭代遍历.md
  • Loading branch information
bqlin committed Feb 19, 2022
1 parent 28cff71 commit a9ae0f5
Showing 1 changed file with 43 additions and 57 deletions.
100 changes: 43 additions & 57 deletions problems/二叉树的迭代遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func inorderTraversal(root *TreeNode) []int {
}
```

javaScript
javaScript

```js

Expand Down Expand Up @@ -454,7 +454,7 @@ var postorderTraversal = function(root, res = []) {
};
```

TypeScript:
TypeScript

```typescript
// 前序遍历(迭代法)
Expand Down Expand Up @@ -509,77 +509,63 @@ function postorderTraversal(root: TreeNode | null): number[] {
};
```

Swift:
Swift

> 迭代法前序遍历
```swift
// 前序遍历迭代法
func preorderTraversal(_ root: TreeNode?) -> [Int] {
var res = [Int]()
if root == nil {
return res
}
var stack = [TreeNode]()
stack.append(root!)
var result = [Int]()
guard let root = root else { return result }
var stack = [root]
while !stack.isEmpty {
let node = stack.popLast()!
res.append(node.val)
if node.right != nil {
stack.append(node.right!)
let current = stack.removeLast()
// 先右后左,这样出栈的时候才是左右顺序
if let node = current.right { //
stack.append(node)
}
if node.left != nil {
stack.append(node.left!)
if let node = current.left { //
stack.append(node)
}
result.append(current.val) //
}
return res
return result
}
```

> 迭代法中序遍历
```swift
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var res = [Int]()
if root == nil {
return res
}
var stack = [TreeNode]()
var cur: TreeNode? = root
while cur != nil || !stack.isEmpty {
if cur != nil {
stack.append(cur!)
cur = cur!.left
} else {
cur = stack.popLast()
res.append(cur!.val)
cur = cur!.right
// 后序遍历迭代法
func postorderTraversal(_ root: TreeNode?) -> [Int] {
var result = [Int]()
guard let root = root else { return result }
var stack = [root]
while !stack.isEmpty {
let current = stack.removeLast()
// 与前序相反,即中右左,最后结果还需反转才是后序
if let node = current.left { //
stack.append(node)
}
if let node = current.right { //
stack.append(node)
}
result.append(current.val) //
}
return res
return result.reversed()
}
```

> 迭代法后序遍历
```swift
func postorderTraversal(_ root: TreeNode?) -> [Int] {
var res = [Int]()
if root == nil {
return res
}
// 中序遍历迭代法
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var result = [Int]()
var stack = [TreeNode]()
stack.append(root!)
// res 存储 中 -> 右 -> 左
while !stack.isEmpty {
let node = stack.popLast()!
res.append(node.val)
if node.left != nil {
stack.append(node.left!)
}
if node.right != nil {
stack.append(node.right!)
var current: TreeNode! = root
while current != nil || !stack.isEmpty {
if current != nil { // 先访问到最左叶子
stack.append(current)
current = current.left //
} else {
current = stack.removeLast()
result.append(current.val) //
current = current.right //
}
}
// res 翻转
res.reverse()
return res
return result
}
```

Expand Down

0 comments on commit a9ae0f5

Please sign in to comment.