题目:

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

Construct the maximum tree by the given array and output the root node of this tree.

Example 1:

Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:6/   \3     5\    / 2  0   \1

Note:

  1. The size of the given array will be in the range [1,1000].

分析:

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

  1. 二叉树的根是数组中的最大元素。
  2. 左子树是通过数组中最大值左边部分构造出的最大二叉树。
  3. 右子树是通过数组中最大值右边部分构造出的最大二叉树。

通过给定的数组构建最大二叉树,并且输出这个树的根节点。

每次选出数组中的最大值作为根节点,左子树就递归执行最大值的左侧,右子树递归执行最大值的右侧。当数组为空的时候返回空指针。

程序:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* constructMaximumBinaryTree(vector<int>& nums) {if(nums.size() == 0) return nullptr;auto it = max_element(nums.begin(), nums.end());TreeNode* root = new TreeNode(*it);vector<int> l(nums.begin(), it); vector<int> r(it+1, nums.end());root->left = constructMaximumBinaryTree(l);root->right = constructMaximumBinaryTree(r);return root;}
};

转载于:https://www.cnblogs.com/silentteller/p/10909388.html

LeetCode 654. Maximum Binary Tree最大二叉树 (C++)相关推荐

  1. 654. Maximum Binary Tree最大二叉树

    网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...

  2. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  3. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树

    反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...

  4. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  5. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  6. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  7. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

  8. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  9. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

最新文章

  1. Redis读写分离技术架构解析
  2. 23个MySQL常用查询语句
  3. 自然语言处理库——TextBlob
  4. 接facebook广告_Facebook广告分析
  5. recycleview 自动循环滚动_划重点 | 不能不知的滚动轴承知识—轴承分类(续)
  6. 若依启动sentinel教程
  7. java1乘到10000_你如何制作一个10000的数组,只有1-1000的值?
  8. [转]我不敢!⋯⋯⋯⋯致所有拼搏的年輕人。
  9. python解释器的提示符是shell嘛_windows python3.2 shell环境(python叫做解释器)
  10. We7 从这里开始---we7的功能和特点(3)
  11. 华旭 身份证读卡器测试软件_华旭身份证读卡器阅读软件8.0版本 支持免驱
  12. Java一个汉字占几个字节(详解与原理)(转载)
  13. Android开发艺术探索之初探AIDL(一)
  14. python图形模块_Python图像处理PIL各模块详细介绍(推荐)
  15. LoRa的码片速率chip rate
  16. 图片大小/像素/分辨率之间有什么关系
  17. html里用js实现随机抽奖,js实现简单随机抽奖的方法
  18. grpc AS编译报No such property: javaCompilerTask for class错误
  19. Word04---字体
  20. 51-C语言-求斐波那契数的前n项和

热门文章

  1. 冒泡排序java代码_美团面试,我竟然输给了冒泡排序。。。
  2. jquery 如何插入元素
  3. spring学习--AOP五个通知
  4. Java中找出s字符串的回文_给定一个字符串 s,找到 s 中最长的回文子串。
  5. java包名命名规则_告别编码5分钟,命名2小时!史上最全的Java命名规范参考!
  6. win10计算机怎么拨号上网,win10拨号连接怎么创建 win10宽带拨号连接如何设置
  7. python扩展库xlwt支持对_python第三方库——xlrd和xlwt操作Excel文件学习
  8. strike F1 to retry boot,F2 for setup utility
  9. 聚类算法实践——PCCA、SOM、Affinity Propagation
  10. 各种数字字体样式_来自中世纪建筑 文化的字体——————哥特式