[題目分析] 計算每層中結點值大於的結點個數應按層次遍歷設一隊列Q用front和rear分別指向隊頭和隊尾元素last指向各層最右結點的位置存放值大於的結點結構為
typedef struct {int levelvalueidx; }node;//元素所在層號值和本層中的序號
node a[]s;
void ValueGT(BiTree bt)//查各層中結點值大於的結點個數輸出其值及序號
{if(bt!=null)
{int front=last=rear=level=i=num=;//num記>的結點個數
BiTree Q[];Q[]=bt;//根結點入隊
while(front<=last)
{bt=Q[++front];
if(bt>data>){slevel=level; sidx=++i; svalue=bt>data; a[++num]=s;}
if(bt>lchild!=null) Q[++rear]=bt>lchild;//左子女入隊列
if(bt>rchild!=null) Q[++rear]=bt>rchild;//右子女入隊列
if(front==last) {last=rear; level++; i=;} //本層最後一個結點已處理完
} //初始化下層last指向下層最右結點層號加下層>的序號初始為
}//while
for(i=;i<=num;i++) //輸出data域數值大於的結點的層號data域的數值和序號
printf(層號=%d本層序號=%d值=%da[i]levela[i]idxa[i]value);
}//算法ValueGT結束
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23722.html