【发布时间】:2017-11-06 02:58:56
【问题描述】:
我尝试使用以下代码将二叉树存储到数组中:
public void inorderArray(int[] array) {
inorderArray(root, array, 0);
}
private static Integer inorderArray(TreeNode root, int[] array, int index) {
// an inorder traversal that stores the items in array
if(root == null){return index;}
inorderArray(root.left, array, index);
array[index++] = root.key;
inorderArray(root.right, array, index);
return index;
}
我不断收到[94, 95, 0, 0, 0, 0, 0, 0, 0, 0],
而且它也不按顺序。
我不知道我做错了什么。任何帮助表示赞赏。
【问题讨论】:
-
附带说明 - 无需返回
Integer,只需int就足够了。
标签: java arrays binary-tree binary-search-tree