二叉树的遍历(递归最好)

不用链表,用顺序表存储。
中序,后序遍历就行。

递归中序遍历,根节点为0, total表示总的长度
// INVALID_VALUE表示无效值,及表示非完全二叉树中的无效节点
void Traversal(int array[], int root, int total)
{
if ((root * 2 + 1) < total && array[root * 2 + 1] != INVALID_VALUE)
Traversal(array, root * 2 + 1, total);
if (array[root] != INVALID_VALUE)
printf("%d ", array[root]);
if ((root * 2 + 2) < total && array[root * 2 + 2] != INVALID_VALUE)
Traversal(array, root * 2 + 2, total);
}
温馨提示:答案为网友推荐,仅供参考
相似回答