存储顺序遍历树C

时间:2013-09-26 07:29:58

标签: c binary-tree

我们如何使用inorder遍历将树的元素存储在数组中? 我读过人们在i处说店铺父母指数而左边孩子2 * i + 1和右边ar 2 * i + 2。但它对我不起作用? 需要帮助:)

1 个答案:

答案 0 :(得分:1)

假设它是二叉树,这里是伪代码

tree_to_array(tree, array, index)
    if tree != NULL then
        // stores recursively all the elements on the left
        index = tree_to_array(tree.left, array, index)
        // the root of the (sub)tree
        array[index] = tree
        // stores recursively all the elements on the right
        return tree_to_array(tree.right, array, index + 1)
    return index