1#include <iostream>2 using namespacestd;3typedefstructnode{4 Chardata;5Node *lchild,*Rchild;6}binode,*Bitree;7 Bitree createtree (bitree root)8 {9 Charch;TenCin>>ch; One if(ch=='#') Aroot=NULL; - Else - { theroot=NewBinode; -Root->data=ch; -Root->lchild=createtree (root->lchild); -Root->rchild=createtree (root->rchild); + } - returnRoot; + } A voidinordertraverse (bitree root) at { - if(root!=NULL) - { -cout<<root->data<<" "; -Inordertraverse (root->lchild); -Inordertraverse (root->rchild); in } - } to intdepth (bitree root) + { - intleft=0, right=0 ; the if(!root) * return 0; $ ElsePanax Notoginseng { -Right=depth (root->rchild); theLeft=depth (root->lchild); + } A returnright>left?right+1: left+1; the } + intcount (bitree root) - { $ intn=0; $ if(!root) - return 0; - Else if(root->lchild==null&&root->rchild==NULL) the return 1; - ElseWuyiN=count (Root->lchild) +count (root->rchild); the returnN; - } Wu intMain () - { AboutBitree root=NULL; $root=Createtree (root); - Inordertraverse (root); -Cout<<depth (Root) <<Endl; -Cout<<count (Root) <<Endl; A}
Get binary Tree Depth leaf number