mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
update 0654.最大二叉树: 更新 go 代码风格
This commit is contained in:
@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
* 确定递归函数的参数和返回值
|
* 确定递归函数的参数和返回值
|
||||||
|
|
||||||
参数就是传入的是存放元素的数组,返回该数组构造的二叉树的头结点,返回类型是指向节点的指针。
|
参数传入的是存放元素的数组,返回该数组构造的二叉树的头结点,返回类型是指向节点的指针。
|
||||||
|
|
||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
@ -309,19 +309,13 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
|
||||||
* type TreeNode struct {
|
|
||||||
* Val int
|
|
||||||
* Left *TreeNode
|
|
||||||
* Right *TreeNode
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
func constructMaximumBinaryTree(nums []int) *TreeNode {
|
func constructMaximumBinaryTree(nums []int) *TreeNode {
|
||||||
if len(nums)<1{return nil}
|
if len(nums) == 0 {
|
||||||
//首选找到最大值
|
return nil
|
||||||
|
}
|
||||||
|
// 找到最大值
|
||||||
index := findMax(nums)
|
index := findMax(nums)
|
||||||
//其次构造二叉树
|
// 构造二叉树
|
||||||
root := &TreeNode {
|
root := &TreeNode {
|
||||||
Val: nums[index],
|
Val: nums[index],
|
||||||
Left: constructMaximumBinaryTree(nums[:index]), //左半边
|
Left: constructMaximumBinaryTree(nums[:index]), //左半边
|
||||||
@ -330,8 +324,8 @@ func constructMaximumBinaryTree(nums []int) *TreeNode {
|
|||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
func findMax(nums []int) (index int) {
|
func findMax(nums []int) (index int) {
|
||||||
for i:=0;i<len(nums);i++{
|
for i, v := range nums {
|
||||||
if nums[i]>nums[index]{
|
if nums[index] < v {
|
||||||
index = i
|
index = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user