把顺序数组转为高度最小的二叉树(算法)

来源:互联网 发布:八字不合 知乎 编辑:程序博客网 时间:2024/05/20 22:38

Given a sorted(increasing order) array,write an algorithm to create a binary tree with minimal height.

public static TreeNode addToTree(int arr[],int start,int end){if(end < start){return null;}int mid = (start + end) /2;TreeNode n = new TreeNode(arr[mid]);n.left = addToTree(arr,start,min - 1);n.right = addToTree(arr,min + 1,end);return n;}public static TreeNode createMinimalBST(int array[]){return addToTree(array,0,array.lenght - 1);}


0 0
原创粉丝点击