class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private TreeNode createBinaryTreeByArray(Integer []array,int index)
{
TreeNode tn = null;
if (index<array.length) {
Integer value = array[index];
if (value == null) {
return null;
}
tn = new TreeNode(value);
tn.left = createBinaryTreeByArray(array, 2*index+1);
tn.right = createBinaryTreeByArray(array, 2*index+2);
return tn;
}
return tn;
}
@Test
public void testLevel() {
Integer[] arr = new Integer[]{3,9,20,null,null,15,7};
TreeNode root = createBinaryTreeByArray(arr,0);
}
java 将数组转化为 二叉树
最新推荐文章于 2025-01-31 21:18:47 发布