二叉樹
阿新 • • 發佈:2017-05-11
print reat value include 遍歷二叉樹 urn tree 中序遍歷 col
#include<stdio.h> #include<iostream> using namespace std; struct BitreeNode{ int value; BitreeNode *left,*right; }; BitreeNode creat(BitreeNode *&t) { int ch; scanf("%d",&ch); if(ch==0) //以0為結束判斷 t=NULL;//根為則判為空樹 else { t=new BitreeNode; t->value=ch; creat(t->left); creat(t->right); } } void xshow(BitreeNode *t)//先序遍歷 { if(t!=NULL) { printf("%d\t",t->value); xshow(t->left); xshow(t->right); } } void zshow(BitreeNode *t)//中序遍歷二叉樹 { if(t!=NULL) { zshow(t->left); printf("%d\t",t->value); zshow(t->right); } } void hshow(BitreeNode *t)////後序遍歷二叉樹 { if(t!=NULL) { hshow(t->left); hshow(t->right); printf("%d\t",t->value); } } int main() { BitreeNode *t; creat(t); xshow(t); zshow(t); hshow(t);return 0; }
二叉樹