-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContents.swift
70 lines (56 loc) · 1.43 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
https://leetcode.com/problems/binary-tree-inorder-traversal/
94. Binary Tree Inorder Traversal
Medium
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
*/
import Foundation
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init(_ val: Int) {
self.val = val
self.left = nil
self.right = nil
}
}
class Solution {
func inorderTraversal(_ root: TreeNode?) -> [Int] {
if root == nil { return [] }
let left = inorderTraversal(root?.left)
let right = inorderTraversal(root?.right)
var array: [Int] = []
array.append(contentsOf: left)
array.append(root!.val)
array.append(contentsOf: right)
return array
}
}
import Foundation
import XCTest
class SolutionTests: XCTestCase {
var solution: Solution!
override func setUp() {
super.setUp()
solution = Solution()
}
func testLeetcodeExample1() {
let node1 = TreeNode(1)
let node2 = TreeNode(2)
let node3 = TreeNode(3)
node1.right = node2
node2.left = node3
XCTAssertEqual(solution.inorderTraversal(node1), [1,3,2])
}
}
SolutionTests.defaultTestSuite.run()