From 59f577d5351e37ae92954ecba1bcde2bb48e67f5 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 20:48:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0654.最大二叉树 go版本 --- problems/0654.最大二叉树.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index ea8c15cd..f0d3e594 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -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;inums[index]{ + index=i + } + } + return +} +``` +