Easy Java Solution


  • 0
    H
    public class Solution {
        public TreeNode constructMaximumBinaryTree(int[] nums) {
            return helper(nums,0,nums.length-1);
        }
    
        public TreeNode helper(int []nums, int start, int end){
        	if(start > end) return null;
    
        	int max = Integer.MIN_VALUE;
        	int index = 0;
        	for(int  i = start; i <= end; i++){
        		if(nums[i] > max) {
        			max = nums[i];
        			index = i;
        		}
        	}
    
        	TreeNode root = new TreeNode(max);
        	root.left = helper(nums,start,index-1);
        	root.right = helper(nums,index+1,end);
        	return root;
        }
    }
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.