当前位置 : 主页 > 手机开发 > harmonyos >

来源:互联网 收集:自由互联 发布时间:2023-08-25
#includestdio.h #includestdlib.h struct ele { int value; ele* left; ele* right; }; void build(ele *p) { int n; scanf("%d",n); if(n==0) p=NULL; else { p=(ele*)malloc(sizeof(ele)); p-value=n; build(p-left); build(p-right); } } void search(ele
#include<stdio.h> 

 #include<stdlib.h> 

 struct ele 

 { 

int value; 

ele* left; 

ele* right; 

 }; 

 void build(ele *&p) 

 { 

int n; 

scanf("%d",&n); 

if(n==0) 

p=NULL; 

else 

{ 

p=(ele*)malloc(sizeof(ele)); 

p->value=n; 

build(p->left); 

build(p->right); 

} 

 } 

 void search(ele*&p) 

 { 

if(p==NULL) 

return; 

else 

{ 

printf("%d 
  ",p->value); 

search(p->left); 

search(p->right); 

} 

 } 

 int sum(ele *&p) 

 { 

if(p==NULL) 

return 0; 

else 

{ 

int m,n; 

m=sum(p->left); 

n=sum(p->right); 

if(m>n) 

return m+1; 

return n+1; 

} 

 } 

 int countsum(ele*&p) 

 { 

if(p==NULL) 

return 0; 

else 

return countsum(p->left)+countsum(p->right)+1; 

 } 

 int main() 

 { 

ele *p; 

build(p); 

search(p); 

printf("%d\n",sum(p)); 

printf("%d\n",countsum(p)); 

return 0; 

 }
上一篇:nyoj 35
下一篇:没有了
网友评论