[Swift]LeetCode654. 最大二叉樹 | Maximum Binary Tree
阿新 • • 發佈:2019-03-06
right put 整數 his elf amp else building public
Runtime: 156 ms Memory Usage: 19.9 MB
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
Input: [3,2,1,6,0,5] Output: return the tree root node representing the following tree: 6 / 3 5 \ / 2 0 1
Note:
- The size of the given array will be in the range [1,1000].
給定一個不含重復元素的整數數組。一個以此數組構建的最大二叉樹定義如下:
- 二叉樹的根是數組中的最大元素。
- 左子樹是通過數組中最大值左邊部分構造出的最大二叉樹。
- 右子樹是通過數組中最大值右邊部分構造出的最大二叉樹。
通過給定的數組構建最大二叉樹,並且輸出這個樹的根節點。
Example 1:
輸入: [3,2,1,6,0,5] 輸入: 返回下面這棵樹的根節點: 6 / 3 5 \ / 2 0 1
註意:
- 給定的數組的大小在 [1, 1000] 之間。
104ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 16 let end = nums.count 17 if (end == 0) { 18 return nil 19 } 20 var nums = nums 21 return construct(&nums, 0, end-1) 22 } 23 24 func construct(_ nums: inout [Int], _ start: Int, _ end: Int) -> TreeNode { 25 var maxNumber = Int.min; var maxIndex = 0 26 27 for i in start...end { 28 if (nums[i] >= maxNumber) { 29 maxNumber = nums[i] 30 maxIndex = i 31 } 32 } 33 let head = TreeNode(maxNumber) 34 35 if (start != end) { 36 switch maxIndex { 37 case start: 38 head.right = construct(&nums, start+1, end) 39 case end: 40 head.left = construct(&nums, start, end-1) 41 default: 42 head.left = construct(&nums, start, maxIndex-1) 43 head.right = construct(&nums, maxIndex+1, end) 44 } 45 } 46 47 return head 48 } 49 }
108ms
1 class Solution { 2 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 3 4 return construct(nums, 0, nums.count) 5 } 6 7 func construct(_ n: [Int], _ s: Int, _ e: Int) -> TreeNode? { 8 if s == e { return nil } 9 10 let idx = max(n, s, e) 11 12 var r = TreeNode(n[idx]) 13 r.left = construct(n, s, idx) 14 r.right = construct(n, idx+1, e) 15 return r 16 } 17 18 func max(_ nums: [Int], _ s: Int, _ e: Int) -> Int { 19 var idx = s 20 for i in s..<e { 21 if nums[idx] < nums[i] { 22 idx = i 23 } 24 } 25 return idx 26 } 27 }
116ms
1 class Solution { 2 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 3 return findMidMax(nums, 0, nums.count - 1) 4 } 5 6 func findMidMax(_ nums:[Int], _ i:Int, _ j:Int) -> TreeNode? { 7 8 if i < 0 || i >= nums.count { 9 return nil 10 } else if j < 0 || j >= nums.count { 11 return nil 12 } else if i > j { 13 return nil 14 } 15 16 17 18 var maxNum = Int.min 19 var maxIndex = -1 20 var index = i 21 while (index <= j) { 22 if maxNum < nums[index]{ 23 maxNum = nums[index] 24 maxIndex = index 25 } 26 index = index + 1 27 } 28 29 var rootNode = TreeNode(nums[maxIndex]) 30 rootNode.left = findMidMax(nums, i, maxIndex - 1) 31 rootNode.right = findMidMax(nums, maxIndex + 1, j) 32 33 return rootNode 34 } 35 }
120ms
1 class Solution { 2 3 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 4 var stack = [TreeNode]() 5 6 for i in 0..<nums.count { 7 var cur = TreeNode(nums[i]) 8 while let last = stack.last, last.val < nums[i] { 9 cur.left = last 10 stack.popLast() 11 } 12 if (!stack.isEmpty) { 13 stack.last?.right = cur 14 } 15 stack.append(cur) 16 } 17 return stack.first 18 } 19 }
132ms
1 class Solution { 2 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 3 if nums.count == 0 { return nil } 4 if nums.count == 1 { return TreeNode(nums[0]) } 5 let maxIdx = getMaxIndex(nums) 6 let tree = TreeNode(nums[maxIdx]) 7 tree.left = constructMaximumBinaryTree(Array(nums.prefix(maxIdx))) 8 if nums.count > maxIdx { 9 tree.right = constructMaximumBinaryTree(Array(nums.suffix(from: maxIdx+1))) 10 } 11 12 return tree 13 } 14 15 func getMaxIndex(_ arr: [Int]) -> Int { 16 var maxIdx = 0 17 for i in 0..<arr.count { 18 if arr[maxIdx] < arr[i] { maxIdx = i } 19 } 20 return maxIdx 21 } 22 }
Runtime: 156 ms Memory Usage: 19.9 MB
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 16 var v:[TreeNode?] = [TreeNode?]() 17 for num in nums 18 { 19 var cur:TreeNode? = TreeNode(num) 20 while(!v.isEmpty && v.last!!.val < num) 21 { 22 cur?.left = v.removeLast() 23 } 24 if !v.isEmpty 25 { 26 v.last!!.right = cur 27 } 28 v.append(cur) 29 } 30 return v.first! 31 } 32 }
160ms
1 class Solution { 2 func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? { 3 guard !nums.isEmpty else { 4 return nil 5 } 6 var root = -1 7 var rootIndex = 0 8 var leftIndex = 0 9 let rightIndex = nums.count - 1 10 while leftIndex <= rightIndex { 11 let left = nums[leftIndex] 12 if left > root { 13 root = left 14 rootIndex = leftIndex 15 } 16 leftIndex += 1 17 } 18 let tree = TreeNode(root) 19 tree.left = constructMaximumBinaryTree(Array(nums[0..<rootIndex])) 20 tree.right = constructMaximumBinaryTree(Array(nums[(rootIndex + 1)...])) 21 return tree 22 } 23 }
[Swift]LeetCode654. 最大二叉樹 | Maximum Binary Tree