Sicily 1210 二叉樹(樹的遍曆)

來源:互聯網
上載者:User

//樹的遍曆<br />//幾個結論<br />//1:前序走訪第一個元素是根,後序遍曆最後一個元素是根<br />//2:前序走訪第二個元素是某子樹的根,但左右不確定<br />//3:在後序遍曆中找到前序走訪的第二個元素,那麼以這個元素為基準,可以劃分新的左右子樹<br />//4:當前序走訪的第二個元素出現在後序遍曆的倒數第二位,以後序遍曆倒數第三位起向左數都是子樹的元素,但是左右不確定,因此有2種情況<br />//遞迴處理 + 乘法原理<br />#include<iostream><br />#include<cstring><br />using namespace std;<br />int ans;<br />char pre[26],post[26];<br />void work(int x1,int y1,int x2,int y2)<br />{<br />int pos;<br />if(x1 >= y1)return;<br />for(int i = x2;i < y2;++i)<br />if(pre[x1+1] == post[i])<br />pos = i;<br />if(pos == y2 - 1)ans *= 2;<br />work(x1+1,x1+1+(pos-x2),x2,pos);<br />work(x1+1+(pos-x2)+1,y1,pos+1,y2-1);<br />}</p><p>int main()<br />{<br />//freopen("in.txt","r",stdin);<br />scanf("%s%s",pre,post);<br />ans = 1;<br />work(0,strlen(pre)-1,0,strlen(post)-1);<br />printf("%d",ans);//這麼奇怪的輸出格式,換了行居然PE了<br />return 0;<br />} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.