假设二叉树用二叉链存储结构:
1.对于“找出根结点到叶子节点之间的路径”提供两种不同的算法:用栈辅助的基于后序非递归遍历的算法,和用队列辅助的基于层次遍历的算法。
2.对于某一给定的节点,求出它的所有祖先:递归求解。
具体实现代码如下:
/*
Author:Ibsen
Date:2015.12.15
*/
#include <iostream>
using namespace std;
const int M=1000;
typedef struct node
{
char data;
struct node *lc,*rc;
}BTree;
void Creat_BTree(BTree * &h,char *str)
{//读取括号表示法表示的二叉树
BTree *st[M],*p;
int pos=0,k,top=