-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path654-MaximumBinaryTree.go
More file actions
112 lines (101 loc) · 4.26 KB
/
654-MaximumBinaryTree.go
File metadata and controls
112 lines (101 loc) · 4.26 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
// 654. Maximum Binary Tree
// You are given an integer array nums with no duplicates.
// A maximum binary tree can be built recursively from nums using the following algorithm:
// Create a root node whose value is the maximum value in nums.
// Recursively build the left subtree on the subarray prefix to the left of the maximum value.
// Recursively build the right subtree on the subarray suffix to the right of the maximum value.
// Return the maximum binary tree built from nums.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/12/24/tree1.jpg" />
// Input: nums = [3,2,1,6,0,5]
// Output: [6,3,5,null,2,0,null,null,1]
// Explanation: The recursive calls are as follow:
// - The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
// - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
// - Empty array, so no child.
// - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
// - Empty array, so no child.
// - Only one element, so child is a node with value 1.
// - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
// - Only one element, so child is a node with value 0.
// - Empty array, so no child.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/12/24/tree2.jpg" />
// Input: nums = [3,2,1]
// Output: [3,null,2,null,1]
// Constraints:
// 1 <= nums.length <= 1000
// 0 <= nums[i] <= 1000
// All integers in nums are unique.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func constructMaximumBinaryTree(nums []int) *TreeNode {
// 找到左边最大的数作为左子树的根节点
// 找到最大的数作为根节点
// 以此类推
if len(nums) == 0 {
return nil
}
var root TreeNode
mx, index := nums[0], 0
for i, v := range nums { // 找到最大的数作为根节点
if mx < v {
mx = v
index = i
}
}
root.Val = mx
root.Left = constructMaximumBinaryTree(nums[:index]) // 找到左边最大的数作为左子树的根节点
root.Right = constructMaximumBinaryTree(nums[index + 1:])
return &root
}
func constructMaximumBinaryTree1(nums []int) *TreeNode {
var build func(left, right int) *TreeNode
build = func(left, right int) *TreeNode {
if right < left {
return nil
}
mx, index := -1, 0
for i := left; i <= right; i++ {
if nums[i] > mx {
mx = nums[i]
index = i
}
}
root := &TreeNode{ Val: nums[index], }
root.Left = build(left, index - 1)
root.Right = build(index + 1, right)
return root
}
return build(0, len(nums) - 1)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/12/24/tree1.jpg" />
// Input: nums = [3,2,1,6,0,5]
// Output: [6,3,5,null,2,0,null,null,1]
// Explanation: The recursive calls are as follow:
// - The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
// - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
// - Empty array, so no child.
// - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
// - Empty array, so no child.
// - Only one element, so child is a node with value 1.
// - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
// - Only one element, so child is a node with value 0.
// - Empty array, so no child.
fmt.Println(constructMaximumBinaryTree([]int{3,2,1,6,0,5})) // &{6 0xc000008060 0xc0000080a8}
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/12/24/tree2.jpg" />
// Input: nums = [3,2,1]
// Output: [3,null,2,null,1]
fmt.Println(constructMaximumBinaryTree([]int{3,2,1})) // &{3 <nil> 0xc000008108}
fmt.Println(constructMaximumBinaryTree1([]int{3,2,1,6,0,5})) // &{6 0xc000008060 0xc0000080a8}
fmt.Println(constructMaximumBinaryTree1([]int{3,2,1})) // &{3 <nil> 0xc000008108}
}