diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index fda64372..ebb5e2a4 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -40,7 +40,7 @@ * 确定递归函数的参数和返回值 -参数就是传入的是存放元素的数组,返回该数组构造的二叉树的头结点,返回类型是指向节点的指针。 +参数传入的是存放元素的数组,返回该数组构造的二叉树的头结点,返回类型是指向节点的指针。 代码如下: @@ -309,30 +309,24 @@ class Solution: ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ func constructMaximumBinaryTree(nums []int) *TreeNode { - if len(nums)<1{return nil} - //首选找到最大值 - index:=findMax(nums) - //其次构造二叉树 - root:=&TreeNode{ + if len(nums) == 0 { + return nil + } + // 找到最大值 + index := findMax(nums) + // 构造二叉树 + root := &TreeNode { Val: nums[index], - Left:constructMaximumBinaryTree(nums[:index]),//左半边 - Right:constructMaximumBinaryTree(nums[index+1:]),//右半边 + Left: constructMaximumBinaryTree(nums[:index]), //左半边 + Right: constructMaximumBinaryTree(nums[index+1:]),//右半边 } return root } -func findMax(nums []int) (index int){ - for i:=0;inums[index]{ - index=i +func findMax(nums []int) (index int) { + for i, v := range nums { + if nums[index] < v { + index = i } } return