一、题目
二、代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution
{
//获得了中序遍历数组 每次取中间构造就可以了
public void build_tree(TreeNode pro, int[] before,int[] after)
{
int left_length = before.length;
int right_length = after.length;
if(left_length!=0)
{
TreeNode left = new TreeNode(before[left_length/2]);
System.out.println("left 节点值 "+left.val);
pro.left = left;
int before_length = left_length/2;
int after_length = left_length -1 - left_length/2;
if(before_length<0) before_length = 0;
if(after_length<0) after_length = 0;
int[] before_left = new int[before_length];
int[] after_left = new int[after_length];
int i;
for(i=0;i<before_length;i++)
{
before_left[i] = before[i];
}
for(i=left_length/2+1;i<left_length;i++)
{
after_left[i-left_length/2-1] = before[i];
}
build_tree(pro.left, before_left, after_left);
}
if(right_length!= 0)
{
TreeNode right = new TreeNode(after[right_length/2]);
System.out.println("right 节点值 "+right.val);
pro.right =right;
int before_length = right_length/2;
int after_length = right_length -1 - right_length/2;
if(before_length<0) before_length = 0;
if(after_length<0) after_length = 0;
int[] before_right = new int[before_length];
int[] after_right = new int[after_length];
int i;
for(i=0;i<before_length;i++)
{
before_right[i] = after[i];
}
for(i=right_length/2+1;i<right_length;i++)
{
after_right[i-right_length/2-1] = after[i];
}
build_tree(pro.right, before_right, after_right);
}
// return pro;
}
public TreeNode sortedArrayToBST(int[] nums)
{
if(nums.length==0) return null;
int length = nums.length;
TreeNode root = new TreeNode(nums[length/2]);
System.out.println("root 节点值 "+root.val);
int before_length = length/2;
int after_length = length -1 - length/2;
if(before_length<0) before_length = 0;
if(after_length<0) after_length = 0;
int[] before = new int[before_length];
int[] after = new int[after_length];
int i;
for(i=0;i<before_length;i++)
{
before[i] = nums[i];
}
for(i=length/2+1;i<length;i++)
{
after[i-length/2-1] = nums[i];
}
build_tree(root, before, after);
return root;
}
}
三、运行结果
四、附录
二刷
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution
{
public TreeNode build(int[] temp)
{
if(temp.length == 0 ) return null;
int i;
int length = temp.length;
int mid = length/2;
int[] left = new int[mid];
int[] right = new int[length-1-mid];
TreeNode root = new TreeNode(temp[mid]);
for(i=0;i<mid;i++) left[i] = temp[i];
for(i=mid+1;i<length;i++) right[i-mid-1] = temp[i];
root.left = build(left);
root.right = build(right);
return root;
}
public TreeNode sortedArrayToBST(int[] nums)
{
int i;
int length;
Arrays.sort(nums);
length = nums.length;
//for(i=0;i<length;i++) System.out.println(" "+nums[i]);
return build(nums);
}
}