654. Maximum Binary Tree
来源:互联网 发布:淘宝定制家具付款流程 编辑:程序博客网 时间:2024/06/01 12:36
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.
----------------------------------------------------------------------------------------
2017/11/25
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return buildBinaryTree(nums,0,nums.length-1);
}
public TreeNode buildBinaryTree(int[] nums,int start,int end){
if(start>end){
return null;
}
int max = nums[start];
int current = start;
for(int i = start+1;i<=end;i++){ //这里注意循环的开始为start+1;
if(nums[i]>max){
max = nums[i];
current = i;
}
}
TreeNode root = new TreeNode(max);
root.left = buildBinaryTree(nums,start,current-1);
root.right = buildBinaryTree(nums,current+1,end);
return root;
}
}
- 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
- ARKit真机测试运行白屏崩溃,dyld:Library not loaded:@rpath.libswiftARKit.dylib
- Day11多态部分-1 【1.1 多态的体现】
- NOIP2017棋盘(普及T3)
- DP(二)2955 Robberies(java版)
- 计算1/1-1/2+1/3-...+1/99-1/100的值
- 654. Maximum Binary Tree
- HDU
- NOIP2017跳房子(普及T4)
- cmake: Enhanced source file handling with target_sources()
- 【Python基础】1.4.4 图形用户实例——用Python实现简易聊天对话框
- 事件监听的几个要点
- 57. Insert Interval
- 推荐《Linux 多线程服务器端编程》
- Day11多态部分-2 【1.2 多态的前提】