-
Notifications
You must be signed in to change notification settings - Fork 0
/
105_construct_binary_tree.swift
50 lines (48 loc) · 2 KB
/
105_construct_binary_tree.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
/**
* 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 {
func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? {
// Solution 2: Second Try
if preorder.count == 0 || inorder.count == 0 { return nil }
var root = TreeNode(preorder[0])
guard let mid = inorder.firstIndex(of: root.val) else { return nil }
root.left = buildTree(Array(preorder[1...mid]), Array(inorder[...mid-1]))
root.right = buildTree(Array(preorder[mid+1...]), Array(inorder[mid+1...]))
return root
// Solution 1: First Try
// var root = TreeNode(preorder[0])
// if preorder == inorder { return root }
// for i in 0..<inorder.count {
// if inorder[i] == preorder[0] {
// var r_inorder : [Int] = []
// var r_preorder : [Int] = []
// var l_inorder : [Int] = []
// var l_preorder : [Int] = []
// if i+1 < inorder.count {
// r_inorder = Array(inorder[i+1...inorder.count-1])
// r_preorder = Array(preorder[preorder.count-r_inorder.count...preorder.count-1])
// root.right = buildTree(r_preorder, r_inorder)
// }
// if 1 < preorder.count {
// l_inorder = Array(preorder[1...preorder.count-r_inorder.count-1])
// l_preorder = Array(inorder[0...i-1])
// root.left = buildTree(l_preorder, l_inorder)
// }
// }
// }
// return root
}
}