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 (0 == num.length) {
            return null;
        }
        if (1 == num.length) {
            return new TreeNode(num[0]);
        }

        return process(num, 0, num.length - 1);
    }

    public TreeNode process(int[] num, int start, int end) {
        if (start > end) {
            return null;
        }
        if (start == end) {
            return new TreeNode(num[start]);
        }
        int mid = start + ((end - start) >> 1);
        TreeNode node = new TreeNode(num[mid]);
        TreeNode leftNode = process(num, start, mid - 1);
        TreeNode rightNode = process(num, mid + 1, end);
        node.left = leftNode;
        node.right = rightNode;
        return node;
    }
}