654. Maximum Binary Tree
来源:互联网 发布:控制鼠标的软件 编辑:程序博客网 时间:2024/06/13 11:57
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
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:
The size of the given array will be in the range [1,1000].
TreeNode* sub_construct(vector<int>& nums, int start, int end){ if (start > end)return NULL; int idx = INT_MIN, pos = 0; for (int i = start; i <= end; i++){ if (idx < nums[i]){ idx = nums[i]; pos = i; } } TreeNode *root = new TreeNode(nums[pos]); root->left = sub_construct(nums, start, pos - 1); root->right = sub_construct(nums, pos + 1, end); return root;}TreeNode* constructMaximumBinaryTree(vector<int>& nums) { if (nums.size() == 0)return NULL; return sub_construct(nums, 0, nums.size() - 1);}
阅读全文
0 0
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- leetcode 654. Maximum Binary Tree
- 【LeetCode】654. Maximum Binary Tree
- [LeetCode]654. Maximum Binary Tree
- leetcode 654. Maximum Binary Tree
- leetcode 654. Maximum Binary Tree
- Leetcode: 654. Maximum Binary Tree
- LeetCode 654. Maximum Binary Tree
- [LeetCode] 654. Maximum Binary Tree
- [leetcode] 654. Maximum Binary Tree
- Linux21个常用高效命令
- C语言数据结构之普通树篇
- 强化练习6:判断一字符串是否为回文,是返回1,不是返回0,出错返回-1
- Java正则表达式
- ActiveReports报表设计器
- 654. Maximum Binary Tree
- uva11235 frequent values(Sparse Table)
- 10、C程序 数组排序之[快速排序法]
- 24之前24之后的使用安卓系统相机的2个总结
- 二、创建第一个图表
- 正则表达式的使用
- js中canvas操作
- ZOJ-3804---YY's Minions (模拟)
- 骑记APP——IOS版登录流程体验报告