获取二叉树深度叶子数

 1 #include <iostream>
 2 using namespace std;
 3 typedef struct node{
 4     char data;
 5     node *lchild,*rchild;
 6 }binode,*bitree;
 7 bitree createTree(bitree root)
 8 {
 9     char ch;
10     cin>>ch;
11     if(ch=='#')
12         root=NULL;
13     else
14     {
15         root=new binode;
16         root->data=ch;
17         root->lchild=createTree(root->lchild);
18         root->rchild=createTree(root->rchild);
19     }
20     return root;
21 }
22 void InOrderTraverse(bitree root)
23 {
24     if(root!=NULL)
25     {
26         cout<<root->data<<" ";
27         InOrderTraverse(root->lchild);
28         InOrderTraverse(root->rchild);
29     }
30 }
31 int depth(bitree root)
32 {
33     int left=0,right=0 ;
34     if(!root)
35         return 0;
36     else
37     {
38         right=depth(root->rchild);
39         left=depth(root->lchild);
40     }
41     return right>left?right+1:left+1;
42 }
43 int count(bitree root)
44 {
45     int n=0;
46     if(!root)
47         return 0;
48     else if(root->lchild==NULL&&root->rchild==NULL)
49         return 1;
50     else
51         n=count(root->lchild)+count(root->rchild);
52     return n;
53 }
54 int main() 
55 {
56     bitree root=NULL;
57     root=createTree(root);
58     InOrderTraverse(root);
59     cout<<depth(root)<<endl;
60     cout<<count(root)<<endl;
61 }

 

posted @ 2015-09-04 22:25  御心飞行  阅读(218)  评论(0编辑  收藏  举报