// 二叉树深度
#include<bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *x,*y;
node( char in=0,node *xx=NULL,node *yy=NULL ):data(in),x(xx),y(yy) {}
};
node* new_tree()
{
node *root=NULL;
char ch; cin>>ch;
if( ch=='#' ) return NULL;
else
{
root=new node( ch );
root->x=new_tree();
root->y=new_tree();
}
return root;
}
int tree_depth( node *root )
{
int x,y;
x=y=0;
if( root!=NULL )
{
x=tree_depth( root->x )+1;
y=tree_depth( root->y )+1;
}
return x>y?x:y;
}
int main()
{
int n;
cin>>n;
while( n-- )
{
node *root=new_tree();
int d=tree_depth( root );
cout<<d<<endl;
}
return 0;
}
模板_二叉树深度
猜你喜欢
转载自blog.csdn.net/qq_63173957/article/details/124954831
今日推荐
周排行