考察点:数学,找规律,

思路:先确定N有几个节点,然后确定左右各有多少棵树,及位置编号,用中序遍历打印左右子树

收获:对于边界 要明确,数组是从1开始编号还是从0开始编号,要统一

经验:对于找规律的题目,要善于运用递归等思想去发现规律

ACcode:

 main()
{
    int n;
    cin
>>n;
    
while(n!=0)
    {
        solve(n);
        cin
>>n;
    }
    
return 0;
}
void solve(int n)
{
    
long temp=0;
    
int nodes=0;
    
while (temp<=n)
    {
        temp
+=a[nodes++];
    }
    nodes
--;
    temp
-=a[nodes];
    n
=n-temp;
    print(n,nodes);
    cout
<<endl;
}
void print(long index,int nodes)
{
    
if (nodes==1)
    {
        cout
<<"X";
        
return;
    }
    
long temp=0;
    
int j=0;    
    
for (int i=0;i<=nodes-1;i++)
    {
        temp
+=a[i]*a[nodes-1-i];
        
if (temp>index)
        {
            temp
-=a[i]*a[nodes-1-i];
            j
=i;
            
break;
        }
    }
    
if (j!=0)
    {
        
int pos1=(index-temp)/a[nodes-1-j];
        cout
<<"(";
        print(pos1,j);
        cout
<<")";
    }
    cout
<<"X";
    
if (nodes-1-j!=0)
    {
        
int pos2=(index-temp)%a[nodes-1-j];
        cout
<<"(";
        print(pos2,nodes
-1-j);
        cout
<<")";        
    }
}

相关文章:

  • 2021-10-04
  • 2022-01-01
  • 2022-03-05
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
猜你喜欢
  • 2021-05-25
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案