# 题干

    3
/ \
9  20
/  \
15   7


# Java代码

package daily;

public class BuildTree {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
//序列长度，两个一样所以只用求一个
int prelen=preorder.length;
return  rebuild(preorder,0,prelen-1,inorder,0,prelen-1);
}
/*
* @Description:
* @Author: ragezor
* @Date: 2020/5/22 10:52 上午
* @Param [preorder, prestart在前序的开始, preend在前序的结束, inorder, instart中序的开始, inend中序的结束]
* @return daily.BuildTree.TreeNode
**/
public TreeNode rebuild(int []preorder,int prestart,int preend,int[] inorder,int instart,int inend){
//空判断
if(prestart>preend||instart>inend){
return  null;
}
//现在的节点
TreeNode root=new TreeNode(preorder[prestart]);
for (int i=inend;i>=instart;i--){
if(inorder[i]==preorder[prestart]){
//左子树长度
int len=i-instart;
//看图对应的两个序列的区间很好懂
root.left=rebuild(preorder, prestart+1, prestart+len, inorder, instart,i-1);
root.right=rebuild(preorder, prestart+len+1, preend, inorder, i+1,inend);
break;

}
}
return  root;
}

}


# 我的leetcode的答案都已经上传到我的git仓库https://github.com/ragezor/leetcode

03-09 78

05-22 55

03-05 43

11-06 33

09-04 65

04-10 202

04-27 29

10-26 17

08-13 118

05-22 50

08-05 149

02-20 87

05-28 3027

02-26 45

09-23 333

11-17 114

07-14 114

12-19 253

07-24 2983

12-21 152

03-19 81万+

04-14 58万+

03-13 14万+

03-01 13万+

03-04 13万+

03-05 5622

03-05 5万+

03-08 4万+

03-08 7万+

03-10 12万+

03-10 18万+

03-12 11万+

03-13 11万+

03-16 10万+

03-18 8519

03-19 8万+

03-20 7316

03-22 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-26 1万+

03-27 4万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 5043

05-28 1万+

04-01 6787

04-02 1万+

04-02 4万+

04-06 7万+

04-07 5万+

04-09 8万+

04-09 2万+

05-17 7298

04-11 3万+

04-15 5万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 5750

04-30 8136

05-16 5万+

05-06 1万+

05-08 4万+

05-11 3万+

05-12 2343

05-12 1万+

05-14 5527

05-16 3079

05-16 1万+

05-18 7533

05-24 3941

05-18 2864

05-19 9624

#### 一个晚上，我学会了24种开发语言！

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试