654. Maximum Binary Tree

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

Example 1:

复制代码
Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
    - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
        - Empty array, so no child.
        - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
            - Empty array, so no child.
            - Only one element, so child is a node with value 1.
    - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
        - Only one element, so child is a node with value 0.
        - Empty array, so no child.

Example 2:

复制代码
Input: nums = [3,2,1]
Output: [3,null,2,null,1]

Constraints:

  • 1 <= nums.length <= 1000

  • 0 <= nums[i] <= 1000

  • All integers in nums are unique.

    class Solution {
    public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    TreeNode*node=new TreeNode(0);
    //是否叶节点
    if(nums.size()==1){
    node->val=nums[0];
    return node;
    }
    //找到数组最大值及其下标
    int maxValue=0;//因为数组元素大于等于1
    int maxValueIndex=0;
    for(int i=0;i<nums.size();i++){
    if(maxValue<nums[i]){
    maxValue=nums[i];
    maxValueIndex=i;
    }
    }
    node->val=maxValue;
    //最大值所在的下标左区间,构造左子树
    if(maxValueIndex>0){
    vector<int>newVec(nums.begin(),nums.begin()+maxValueIndex);
    node->left=constructMaximumBinaryTree(newVec);
    }
    //最大值所在的下标右区间,构造右子树
    if(maxValueIndex<nums.size()-1){
    vector<int>newVec(nums.begin()+maxValueIndex+1,nums.end());
    node->right=constructMaximumBinaryTree(newVec);
    }

    复制代码
          return node;
      }

    };

注意:

1,前提条件

1)二叉树

2,解题流程

PS :这里的代码及其流程存在缺陷,与 106/105 题一样,重复定义过多 vector ,浪费空间,后续熟练之后再进行优化,这个版本代码比较利于理解

1)先做,后做

  • 第一步,梳理题意,这道题本身不是很难,主要的难点就是如何判断哪一部分构造左子树,哪一部分构造右子树。
    • 第二步,new一个node存储数组最大值为根节点
    • 第三步,判断叶节点
      • 也就是nums的size==1
      • 不要忘记给node赋值和return
    • 第四步,找到数组的最大值及其下标
      • 把maxValue的初始值设为0(原因:数组元素大于等于1),maxValueIndex也为0
      • for循环找到maxValue和MaxValueIndex
      • 给node赋值
    • 第五步,根据最大值左区间,构造左子树
      • 条件:MaxValue>0
      • 新数组,newVec(begin,begin+MaxValueIndex)
      • 递归
    • 第六步,根据最大值右区间,构造右子树
      • 条件:MaxValue<nums.size()-1
      • 新数组,newVec(begin+MaxValueIndex+1,end)
      • 递归
    • 第七步,return node

2)知识点套路

  • 递归
  • 判断叶节点
  • 判断左右子树及其范围

3)前提

4)注意点

  • 判断完叶节点后不要忘记给node赋值
  • for循环找到MaxValue和MaxValueIndex之后也要赋值
相关推荐
知来者逆18 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊22 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北23 分钟前
力扣-160.相交链表
算法·leetcode·链表
草莓啵啵~1 小时前
搜索二叉树-key的搜索模型
数据结构·c++
爱研究的小陈1 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing1 小时前
二叉树的最大宽度计算
算法·面试
丶Darling.2 小时前
26考研 | 王道 | 数据结构 | 第八章 排序
数据结构·考研·排序算法
BB_CC_DD2 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
我也不曾来过12 小时前
list底层原理
数据结构·c++·list
梁下轻语的秋缘3 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯