Java 递归分治法
将升序数组转化为平衡二叉搜索树
http://www.nowcoder.com/questionTerminal/7e5b00f94b254da599a9472fe5ab283d
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param num int整型一维数组 * @return TreeNode类 */ public TreeNode sortedArrayToBST (int[] num) { // write code here if(num == null){ return null; } return helper(num, 0, num.length - 1); } private TreeNode helper(int[] num, int left, int right){ if(left > right){ return null; } int mid = left + (right - left + 1) / 2; TreeNode lNode = helper(num, left, mid - 1); TreeNode rNode = helper(num, mid + 1, right); TreeNode node = new TreeNode(num[mid]); if(lNode != null) node.left = lNode; if(rNode != null) node.right = rNode; return node; } }