forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructBinaryTreePreorderInorder.swift
49 lines (42 loc) · 1.66 KB
/
ConstructBinaryTreePreorderInorder.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
/**
* Question Link: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
* Primary idea: Always use the first element in preorder as root,
* then find that one in inorder to get left and right subtrees
* Time Complexity: O(n), Space Complexity: O(1)
*
* Definition for a binary tree node.
* 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 ConstructBinaryTreePreorderInorder {
func buildTree(preorder: [Int], _ inorder: [Int]) -> TreeNode? {
guard preorder.count > 0 && inorder.count > 0 && preorder.count == inorder.count else {
return nil
}
return _buildHelper(preorder, 0, preorder.count - 1, inorder, 0, inorder.count - 1)
}
private func _buildHelper(preorder: [Int], _ preStart: Int, _ preEnd: Int, _ inorder: [Int], _ inStart: Int, _ inEnd: Int) -> TreeNode? {
guard preStart <= preEnd && inStart <= inEnd else {
return nil
}
let root = TreeNode(preorder[preStart])
var mid = 0
for i in inStart ... inEnd {
if inorder[i] == preorder[preStart] {
mid = i
break
}
}
root.left = _buildHelper(preorder, preStart + 1, preStart + mid - inStart, inorder, inStart, mid - 1)
root.right = _buildHelper(preorder, preStart + mid - inStart + 1, preEnd, inorder, mid + 1, inEnd)
return root
}
}