mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
添加 0654.最大二叉树 go版本
添加 0654.最大二叉树 go版本
This commit is contained in:
@ -278,6 +278,39 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
> 654. 最大二叉树
|
||||
|
||||
```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{
|
||||
Val: nums[index],
|
||||
Left:constructMaximumBinaryTree(nums[:index]),//左半边
|
||||
Right:constructMaximumBinaryTree(nums[index+1:]),//右半边
|
||||
}
|
||||
return root
|
||||
}
|
||||
func findMax(nums []int) (index int){
|
||||
for i:=0;i<len(nums);i++{
|
||||
if nums[i]>nums[index]{
|
||||
index=i
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user