/**
* 10 0
* / \
* 5 -3 1-2
* / \ \
* 3 2 11 3-6
* / \ \
* 3 -2 1 7-14
*
* @
param trees
* @
return */
public static TreeNode initTreeNode(Integer[] trees, int n) {
if(n >= trees.length){
return null;
}
if(null == trees[n]) {
return null;
}
int l = n * 2 + 1;
if(l > trees.length){
return new TreeNode(trees[n],null,null);
}
TreeNode treeNode = new TreeNode(trees[n],initTreeNode(trees,2*n+1),initTreeNode(trees,2*n+2));
return treeNode;
}
public static void main(String[] args) {
Integer[] arr = {10, 5, -3, 3, 2, null, 11, 3, -2, null, 1};
TreeNode treeNode = initTreeNode(arr, 0);
}
以前写的一个工具类