-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
51 lines (45 loc) · 1.42 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Foundation
/**
94. Binary Tree Inorder Traversal
Tags: Hash Table、Stack、Tree
https://leetcode.com/problems/binary-tree-inorder-traversal/description/
*/
/// 递归实现. Runtime: 12ms
class SolutionRecursive {
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var result: [Int] = []
_inorderTraversal(root, &result)
return result
}
private func _inorderTraversal(_ node: TreeNode?, _ result: inout [Int]) {
if let node = node {
_inorderTraversal(node.left, &result)
result.append(node.val)
_inorderTraversal(node.right, &result)
}
}
}
/// 用栈模拟递归调用先后顺序实现. Runtime: 16ms
class SolutionStack {
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var result: [Int] = []
var stack: [TreeNode] = []
var curNode = root
while !stack.isEmpty || curNode != nil {
if curNode == nil {
// 叶子节点遍历完了, 出栈
curNode = stack.popLast()
} else if let left = curNode?.left {
stack.append(curNode!)
curNode = left
continue
}
if curNode != nil {
// 输出当前节点后接着遍历右节点
result.append(curNode!.val)
curNode = curNode!.right
}
}
return result
}
}