Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 4555ee8

Browse files
aQuaaQua
aQua
authored and
aQua
committed
654 accepted. 112ms > 106ms
1 parent 7eced7e commit 4555ee8

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Algorithms/0654.maximum-binary-tree/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Output: return the tree root node representing the following tree:
1919
6
2020
/ \
2121
3 5
22-
\ /
23-
2 0
22+
\ /
23+
2 0
2424
\
2525
1
2626
```

Algorithms/0654.maximum-binary-tree/maximum-binary-tree.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ import (
77
type TreeNode = kit.TreeNode
88

99
func constructMaximumBinaryTree(nums []int) *TreeNode {
10+
if len(nums) == 0 {
11+
return nil
12+
}
1013

11-
return nil
14+
if len(nums) == 1 {
15+
return &TreeNode{Val: nums[0]}
16+
}
17+
18+
v, i := getMax(nums)
19+
20+
return &TreeNode{
21+
Val: v,
22+
Left: constructMaximumBinaryTree(nums[:i]),
23+
Right: constructMaximumBinaryTree(nums[i+1:]),
24+
}
25+
}
26+
27+
func getMax(nums []int) (val, index int) {
28+
val = nums[0]
29+
index = 0
30+
for i := 1; i < len(nums); i++ {
31+
if val < nums[i] {
32+
val = nums[i]
33+
index = i
34+
}
35+
}
36+
return val, index
1237
}

Algorithms/0654.maximum-binary-tree/maximum-binary-tree_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ var tcs = []struct {
1515
post []int
1616
}{
1717

18-
// 可以有多个 testcase
18+
{
19+
[]int{3, 2, 1, 6, 0, 5},
20+
[]int{1, 2, 3, 0, 5, 6},
21+
},
22+
23+
// 可以有多个 testcase
1924
}
2025

2126
func Test_fn(t *testing.T) {

0 commit comments

Comments
 (0)