zoukankan      html  css  js  c++  java
  • 获取二叉树深度叶子数

     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 }
  • 相关阅读:
    android启动模式2
    acvitity的日常 启动模式(上)
    Fragment 切换问题
    异常处理
    Xutils的使用 转载 带自己细细研究
    hibernate 增删改
    OGNL
    JDBC
    Struts 文件的上传与下载
    ActionContext和ServletActionContext小结
  • 原文地址:https://www.cnblogs.com/a1225234/p/4782483.html
Copyright © 2011-2022 走看看