-
Notifications
You must be signed in to change notification settings - Fork 2
/
94.Binary-Tree-Inorder-Traversal.swift
52 lines (47 loc) · 1.35 KB
/
94.Binary-Tree-Inorder-Traversal.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
52
//: 94 Binary Tree Inorder Traversal
/**
* Question Link: https://leetcode.com/problems/binary-tree-inorder-traversal/
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
// recursion
func inorderTraversal(_ root: TreeNode?) -> [Int] {
guard let root = root else { return [] }
var result = [Int]()
result += inorderTraversal(root.left)
result.append(root.val)
result += inorderTraversal(root.right)
return result
}
// iteration, using stack to store the parent node
func inorderTraversal2(_ root: TreeNode?) -> [Int] {
var current = root
var stack = [TreeNode]()
var result = [Int]()
while current != nil || !stack.isEmpty {
while let node = current {
stack.append(node)
current = node.left
}
if let node = stack.popLast() {
result.append(node.val)
current = node.right
}
}
return result
}
}