如何打印二叉树的所有路径
给定一个满二叉树,所有左子树的边的权值为0,所有右子树的边的权值为1,要求从根节点开始,一直走到叶子节点,所经过的路径序列。
比如一棵深度为4的满二叉树,共有8条这样的路径:
(0,0,0)
(0,0,1)
(0,1,0)
(0,1,1)
(1,0,0)
(1,0,1)
(1,1,0)
(1,1,1)
应该不难的,搞了半天,请高人指教!100分奉上!
[解决办法]
看样子不要管二叉树,直接 000, 001, 010...这样就行了
深度为5就0000, 0001, 0010, 0011...
没认真看题目,其实是没看懂,只看了那个二进制的0, 1, 2...8
[解决办法]
#include "stdio.h "
#include "stdlib.h "
typedef struct BiTNode{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}*BiTree;
BiTree T;
void CreateBiTree(BiTree &T);
void preorder(BiTree T);
void order(BiTree T);
void postorder(BiTree T);
void main()
{ int t;
printf( "\n本程序实现二叉树的操作。 ");
printf( "\n可以进行建立二叉树,递归先序,中序,后序遍历等操作。 ");
printf( "\n请将先序遍历二叉树的结果输入以建立二叉树。 ");
printf( "\n对于叶子结点以空格表示。 ");
printf( "\n例如:abc de g f <回车> ,建立如下二叉树:\n ");
printf( " a \n ");
printf( " / \n ");
printf( " b \n ");
printf( " / \\ \n ");
printf( "c d \n ");
printf( " / \\ \n ");
printf( " e f \n ");
printf( " \\ \n ");
printf( " g \n ");
printf( "建立一个二叉树 ");
CreateBiTree(T);
printf( "\n1.先序遍历 ");
printf( "\n2.中序遍历 ");
printf( "\n3.后序遍历 ");
printf( "\n4.退出程序 ");
printf( "\n进行选择 ");
scanf( "%d ",&t);
while(t <=3)
{ switch(t)
{ case 1:{printf( "\n递归的先序遍历为 ");preorder(T);break;}
case 2:{printf( "\n递归的中序遍历为 ");order(T);break;}
case 3:{printf( "\n递归的后序遍历为 ");postorder(T);break;}
case 4:exit(1);}
printf( "\n再次进行选择 ");
scanf( "%d ",&t);
}}
void CreateBiTree(BiTree &T)
{ //按次序输入二叉树中的结点的值(一个字符),空格字符表示空树,
//构造二叉链表表示的二叉树T.
char ch;
// printf( "\n输入一个字符 ");
T=(BiTree)malloc(sizeof(BiTNode));//生成根结点
scanf( "%c ",&ch);
if(ch== ' ') T=NULL;
else
{ T-> data=ch;
CreateBiTree(T-> lchild);//构造右子树
CreateBiTree(T-> rchild);//构造左子树
}}
void preorder(BiTree T)//先序遍历
{
if(T!=NULL)
{ printf( "%c ",T-> data);
preorder(T-> lchild);
preorder(T-> rchild);
}}
void order(BiTree T)//中序遍历
{if(T!=NULL)
{
order(T-> lchild);
printf( "%c ",T-> data);
order(T-> rchild);
}}
void postorder(BiTree T)//后序遍历
{ if(T!=NULL)
{
postorder(T-> lchild);
postorder(T-> rchild);
printf( "%c ",T-> data);
}}
[解决办法]
路过,顶一个
[解决办法]
#include "iostream "
#include "vector "
using namespace std;
struct Node
{
Node* pLeft;
Node* pRight;
Node()
{
pLeft = NULL;
pRight = NULL;
}
Node(Node* l, Node* r)
{
pLeft = l;
pRight = r;
}
};
Node* CreateTestTree()
{
Node* pRoot = new Node(new Node(new Node, new Node), new Node(new Node, new Node));
return pRoot;
}
void DeleteTree(Node* pNode)
{
if (!pNode)
return;
DeleteTree(pNode-> pLeft);
DeleteTree(pNode-> pRight);
delete pNode;
}
void PrintTree(Node* pNode, vector <int> & query)
{
if (!pNode-> pLeft && !pNode-> pRight)
{
cout < < "( ";
for (size_t i = 0; i < query.size(); i++)
{
cout < <query[i];
if (i < query.size() - 1)
cout < < ", ";
}
cout < < " ) " < <endl;
return;
}
if (pNode-> pLeft)
{
vector <int> lq = query;
lq.push_back(0);
PrintTree(pNode-> pLeft, lq);
}
if (pNode-> pRight)
{
vector <int> rq = query;
rq.push_back(1);
PrintTree(pNode-> pRight, rq);
}
}
void main()
{
Node* pRoot = CreateTestTree();
PrintTree(pRoot, vector <int> ());
DeleteTree(pRoot);
pRoot = NULL;
system( "pause ");
}