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 }
  • 相关阅读:
    头部尾部始终处于两端(适用于pc端和移动端)
    运用active和hover实现导航栏的页面切换
    POJ1423-阶乘的位数-Big Number
    大数阶乘
    n皇后
    4103:踩方格
    2815:城堡问题
    特殊回文数
    十六进制转十进制
    十六进制转八进制
  • 原文地址:https://www.cnblogs.com/a1225234/p/4782483.html
Copyright © 2011-2022 走看看