1
/ \
2 3
/ \ / \
4 5 6 7
与えられた二分木に対して、1は2の祖先であるため、a [2] [1]=1のような祖先プロパティを満たす行列a[7][7]を作成する必要があります...。
私は余分なスペースと配列を使用してそれを解決しました...私が思いついた解決策は
int a[n][n]={0};
void updatematrix(int a[][n],struct node *root,int temp[],int index){
if(root == NULL)
return ;
int i;
for(i=0;i< index;i++)
a[root->data][temp[i]]=1;
temp[index]=root->data;
updatematrix(a,root->left,temp,index+1);
updatematrix(a,root->right,temp,index+1);
}
私の解決策に間違いはありますか?これをインプレースで実行できますか???(つまり、一時配列を使用せずに)