654. Maximum Binary Tree
来源:互联网 发布:国人欣赏水平 知乎 编辑:程序博客网 时间:2024/06/06 13:08
- 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.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) { int i = 0; int max = 0; struct TreeNode * root = NULL; if(numsSize <= 0){ return NULL; } max = 0; for(i = 1;i < numsSize; ++i){ if(nums[i]>nums[max]){ max = i; } } root = (struct TreeNode *)malloc(sizeof(struct TreeNode)); root->val = nums[max]; root->left = constructMaximumBinaryTree(nums,max); root->right = constructMaximumBinaryTree(nums+max+1,numsSize - max -1); return root;}
阅读全文
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
- leetcode 231. Power of Two
- POJ-3233:Matrix Power Series
- Android Fragment
- python中self意义
- 【数据库】通过项目再学习sqlite
- 654. Maximum Binary Tree
- LeetCode算法学习日志-638 Shopping Offers
- linux文件权限
- 近期三日的网络爬虫和API的小结
- Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface
- listview适配器 优化
- WOJ1137-Flowers
- HDU3466 Proud Merchants 排序01背包
- 数据库操作练习11