【发布时间】:2017-01-18 14:38:30
【问题描述】:
我参考了以下链接,看看如何将 inOrder 遍历保存到数组 Binary Search Tree to inOrder Array。
我的树:
100
/ \
50 300
/ \
20 70
当第一个元素 (20) 被插入到数组中时,索引值增加为 1。现在,当控制去获取下一个节点 (50) 时,索引值变为 0。
代码:
storeInOrder(root1,arr1,0);
private static void storeInOrder(Node root1, int[] arr1, int index) {
if(root1 == null){return;}
storeInOrder(root1.left, arr1,index);
arr1[index++] = root1.data;
storeInOrder(root1.right,arr1,index);
}
数组中的预期输出:20 50 70 100 300
我得到的输出为 100 300 0 0 0
【问题讨论】:
-
你用什么语言写这个?您的 index 参数似乎不是通过引用传递的。
-
@TeddySterne 我正在用 Java 编写代码
标签: arrays binary-search-tree inorder