LeetCode654. 最大二叉树Golang版

LeetCode654. 最大二叉树Golang版

1. 问题描述

给定一个不含重复元素的整数数组 nums 。一个以此数组直接递归构建的 最大二叉树 定义如下:

二叉树的根是数组 nums 中的最大元素。
左子树是通过数组中 最大值左边部分 递归构造出的最大二叉树。
右子树是通过数组中 最大值右边部分 递归构造出的最大二叉树。
返回有给定数组 nums 构建的 最大二叉树 。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 思路

递归构造二叉树模板题

  1. 找到当前节点索引index,根据该索引构造出node
  2. 根据数组递归构建二叉树
	node.Left = constructMaximumBinaryTree(nums[:index])
    node.Right = constructMaximumBinaryTree(nums[index+1:])

3. 代码

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func constructMaximumBinaryTree(nums []int) *TreeNode {
    return maxTree(nums, 0, len(nums))
}

func maxTree(nums []int, left int, right int) *TreeNode {
    if left >= right {
        return nil
    }

    maxIndex := left
    for i := left + 1; i < right; i++ {
        if nums[i] > nums[maxIndex] {
            maxIndex = i
        }
    }
    root := &TreeNode {
        Val : nums[maxIndex],
        Left : nil,
        Right : nil,
    }

    root.Left = maxTree(nums, left, maxIndex )
    root.Right = maxTree(nums, maxIndex + 1, right)
    return root
}

// func findMax(nums []int, left int, right int) int {
//     max := nums[left]
//     maxIndex := left
//     for i := left+1; i <= right; i++ {
//         if nums[i] > max {
//             max = nums[i]
//             maxIndex = i
//         }
//     }
//     return maxIndex
// }

3.2. 另一种写法

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func constructMaximumBinaryTree(nums []int) *TreeNode {
    return maxTree(nums, 0, len(nums) - 1)
}

func maxTree(nums []int, left int, right int) *TreeNode {
    if left > right {
        return nil
    }

    maxIndex := left
    for i := left + 1; i <= right; i++ {
        if nums[i] > nums[maxIndex] {
            maxIndex = i
        }
    }
    root := &TreeNode {
        Val : nums[maxIndex],
        Left : nil,
        Right : nil,
    }

    root.Left = maxTree(nums, left, maxIndex - 1 )
    root.Right = maxTree(nums, maxIndex + 1, right)
    return root
}

3.3. 构造二叉树模板写法

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func constructMaximumBinaryTree(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    index := max(nums)

    node := &TreeNode {
        Val : nums[index],
    }
    node.Left = constructMaximumBinaryTree(nums[:index])
    node.Right = constructMaximumBinaryTree(nums[index+1:])
    return node
}

func max(nums []int) int {
    var maxIndex int
    for i := 1; i < len(nums); i++ {
        if nums[i] > nums[maxIndex] {
            maxIndex = i
        }
    }
    return maxIndex
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值