abc,,de,g,,f,,,
cbegdfa cgefdba 3 5
#include <iostream> using namespace std; typedef char Elem_Type; typedef struct BiTNode { Elem_Type data; BiTNode *lchild; BiTNode *rchild; }BiTNode; void CreateBiTree(BiTNode **root) { Elem_Type temp; cin>>temp; if(temp == ‘,‘) *root = NULL; else { *root = new BiTNode; (*root)->data = temp; CreateBiTree( &(*root)->lchild ); CreateBiTree( &(*root)->rchild ); } } void InOrderTraverse(BiTNode *root)//中 { if( root ) { InOrderTraverse( root->lchild); cout<<root->data; InOrderTraverse( root->rchild); } } void PostOrderTraverse(BiTNode *root) { if( root ) { PostOrderTraverse( root->lchild); PostOrderTraverse( root->rchild); cout<<root->data; } } int LeafNodes( BiTNode *root) { static int count =0; if( !root ) return 0; if( !root->lchild && !root->rchild) count++; LeafNodes(root->lchild); LeafNodes(root->rchild); return count; } int BiTreeDepth(BiTNode *root) { if( !root ) return 0; return (BiTreeDepth(root->lchild) > BiTreeDepth(root->rchild)? BiTreeDepth(root->lchild) : BiTreeDepth(root->rchild)) + 1; } int main(void) { BiTNode *root = NULL; CreateBiTree(&root); InOrderTraverse(root); cout<<endl; PostOrderTraverse(root); cout<<endl; cout<<LeafNodes( root)<<endl; cout<<BiTreeDepth(root)<<endl; return 0; } /************************************** Problem id : SDUT OJ 2136 User name : 李俊 Result : Accepted Take Memory : 456K Take Time : 10MS Submit Time : 2014-05-05 23:13:18 **************************************/
二叉树的建立与遍历(山东理工OJ),布布扣,bubuko.com
原文地址:http://blog.csdn.net/li_jun_09_05/article/details/25082931