码迷,mamicode.com
首页 > 其他好文 > 详细

获取二叉树深度叶子数

时间:2015-09-04 23:54:05      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

获取二叉树深度叶子数

标签:

原文地址:http://www.cnblogs.com/a1225234/p/4782483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!