{"id":114,"date":"2013-03-26T18:38:51","date_gmt":"2013-03-26T10:38:51","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=114"},"modified":"2013-03-26T18:38:51","modified_gmt":"2013-03-26T10:38:51","slug":"1020-tree-traversals-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=114","title":{"rendered":"1020. Tree Traversals (25)"},"content":{"rendered":"<p>Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. For each case, the first line gives a positive integer N (&lt;=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>7\n2 3 1 5 7 6 4\n1 2 3 4 5 6 7<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>4 1 6 3 5 7 2<\/pre>\n<p>============================================================<br \/>\n\u4f20\u8bf4\u4e2d\u6b63\u786e\u7387\u6bd4\u8f83\u9ad8\u7684\u4e00\u9053\u9898\uff0c\u67090.5\u54c8\u54c8<br \/>\n\u505a\u8d77\u6765\u5374\u53d1\u73b0\u6570\u636e\u7ed3\u6784\u6982\u5ff5\u5fd8\u5149\u5149\uff0c\u540e\u5e8f\u904d\u5386\u4ec0\u4e48\u7684\u4e00\u6982\u5fd8\u6389\u4e86\uff0c\u767e\u5ea6\u767e\u79d1\u4e86\u4e00\u4e0b\u7ec8\u4e8e\u61c2\u5f97\u4e86..<br \/>\n\u9898\u76ee\u4e5f\u6ca1\u4ec0\u4e48\u6280\u5de7\uff0c\u7ed9\u5b9a\u4e24\u79cd\u987a\u5e8f\uff0c\u80af\u5b9a\u80fd\u6392\u51fa\u4e8c\u53c9\u6811\u7684<br \/>\n\u8981\u6c42\u8f93\u51fa\u5c42\u6b21\u7ed3\u6784\u5c31\u66f4\u597d\u529e\u4e86&#8230;<br \/>\n\u56e0\u4e3a\u9898\u76ee\u7b80\u5355\u6240\u4ee5\u591a\u8bb0\u4e0b\u4e00\u70b9\uff0c\u6015\u81ea\u5df1\u4e07\u4e00\u78b0\u5230\u5fd8\u6389\u4e86\u3002<br \/>\n============================================================<br \/>\n\u539f\u7406\uff1a<br \/>\n\u540e\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u662fL-R-D\uff0c\u540e\u9762\u5047\u5b9a\u8fd9\u4e2a\u8f93\u5165\u5e8f\u5217\u4e3aA\u5e8f\u5217<br \/>\n\u4e2d\u5e8f\u904d\u5386\u662fL-D-R\u987a\u5e8f\uff0c\u5047\u5b9a\u4e3aB\u5e8f\u5217<br \/>\n\u5e8f\u5217\u904d\u5386\u65f6\u5176\u5b9e\u662f\u9012\u5f52\u7684\u611f\u89c9\u3002<br \/>\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u540e\u5e8f\u904d\u5386\u987a\u5e8f\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u80af\u5b9a\u662fD\u8282\u70b9\u65e0\u8bef\uff08\u4e00\u68f5\u6811\u4e0d\u4f1a\u6ca1\u6709\u6839\uff0c\u9664\u975e\u662f\u7a7a\u6811\uff09<br \/>\n\u6240\u4ee5\u7ed9\u5b9aA\u3001B\u5e8f\u5217\uff0c\u628aA\u5e8f\u5217\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u653e\u5230B\u5e8f\u5217\u4e2d\uff0c\u90a3\u4e48B\u5e8f\u5217\u5c31\u80fd\u88ab\u5206\u5272\u6210\u5de6\u4e2d\u53f3\u4e09\u4e2a\u90e8\u5206\uff0c<br \/>\n\u4e2d\u95f4\u90a3\u4e2a\u8282\u70b9\uff0c\u4e2d\u95f4\u8282\u70b9\u7684\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u3002<br \/>\n\u4e8e\u662f\u4e5f\u6210\u4e3a\u4e86\u4e2a\u9012\u5f52\u7684\u8fc7\u7a0b\uff0c\u6bcf\u6b21\u5206\u5272\u5b8c\u6210\u540e\uff0c\u628a\u5de6\u53f3\u5b57\u6570\u518d\u627e\u4ed6\u4eec\u5bf9\u5e94\u7684D\u8282\u70b9\uff08\u4ece\u5e8f\u5217A\u4e2d\u770b\uff09\uff0c\u5c31\u80fd\u5230\u4e16\u754c\u7684\u5c3d\u5934\u4e86\u3002<br \/>\n============================================================<br \/>\n\u4f2a\u4ee3\u7801\uff1a<\/p>\n<pre>\n\u5de5\u4f5c\u961f\u5217\uff1aW\n\u8f93\u5165\u5e8f\u5217\uff1aA, B\n\u5c06B\u6574\u4e2a\u961f\u5217\u538b\u5165W\nwhile(\u5de5\u4f5c\u961f\u5217\u975e\u7a7a)\n{\n    \u53d6\u51fa\u5de5\u4f5c\u961f\u5217\u7684\u961f\u9996\u5143\u7d20\uff08\u4e5f\u662f\u4e00\u4e2a\u961f\u5217\uff09frontList\n    \u5f39\u51fa\u961f\u9996\u5143\u7d20\uff08c++\u91cc\u9762\u53d6top()\u6216\u8005front()\u548cpop()\u662f\u4e24\u4e2a\u8fc7\u7a0b\uff09\n    \u627e\u51fafrontList\u4e2d\u4f4d\u4e8eA\u5217\u6700\u540e\u6b21\u5e8f\u7684\u5143\u7d20m\n    \u6253\u5370m\n    \u5c06frontList\u4e2d\u6392\u5728m\u5143\u7d20\u5de6\u4fa7\u7684\u90a3\u4e9b\u5143\u7d20\u7ec4\u6210\u65b0\u5217\u8868\uff0c\u538b\u5165W\u961f\u5217\n    \u5c06frontList\u4e2d\u6392\u5728m\u5143\u7d20\u53f3\u4fa7\u7684\u90a3\u4e9b\u5143\u7d20\u7ec4\u6210\u65b0\u5217\u8868\uff0c\u538b\u5165W\u961f\u5217\n}\n<\/pre>\n<p>==============================================================<br \/>\n\u4ee3\u7801\uff08\u9ad8\u624b\u52ff\u55b7\uff0c\u6548\u7387\u8f83\u4f4e\u4f46\u662f\u5728PAT\u4e2d\u8017\u65f60ms,\u5185\u5b58\u5360\u7528750k+-\uff09\uff1a<\/p>\n<pre>\n#include <iostream>\n#include <queue>\nusing namespace std;\nint N;\nint* A; \/\/\u540e\u5e8f\u904d\u5386\u987a\u5e8f\nint* B; \/\/\u4e2d\u5e8f\u904d\u5386\u987a\u5e8f\nstruct QueueElem\n{\n\t\/\/\u538b\u5230\u5de5\u4f5c\u961f\u5217\u4e2d\u7684\u5143\u7d20\uff08\u5176\u5b9e\u4e5f\u662f\u4e2a\u961f\u5217\uff09\n\tint capacity;\n\tint curSize;\n\tint* elements;\n\tQueueElem(int _capacity)\n\t{\n\t\tcapacity = _capacity;\n\t\tcurSize = 0;\n\t\telements = new int[capacity];\n\t}\n\tvoid addElem(int e)\n\t{\n\t\tif(curSize >= capacity )\n\t\t{\n\t\t\tcerr << \"Fail to insert. Size exceeded!\" << endl;\n\t\t\treturn;\n\t\t}\n\t\telements[curSize++] = e;\n\t}\n};\nqueue<queueElem> W; \/\/\u5de5\u4f5c\u961f\u5217\nint findLastOnePos(QueueElem e)\n{\n\t\/\/\u5bfb\u627eQueueElem\u5143\u7d20\u4e2d\u5904\u5728A\u5217\u6b21\u5e8f\u6700\u540e\u7684\u90a3\u4e2a\u5143\u7d20\n\t\/\/\u8fd4\u56de\u503c\u4e3a\u8be5\u5143\u7d20\u5728\u3010e\u5217\u8868\u3011\u4e2d\u7684\u4f4d\u7f6e\uff01\n\t\/\/2\u5c42\u5faa\u73af\u6548\u7387\u8f83\u4f4e..\n\tint lastIndexA = -1;\n\tint lastIndexE = -1;\n\tfor(int i=0; i<e.curSize; i++)\n\t{\n\t\tfor(int posA=0; posA<n; posA++)\n\t\t{\n\t\t\tif(e.elements[i] == A[posA] &#038;&#038; posA > lastIndexA)\n\t\t\t{\n\t\t\t\tlastIndexA = posA;\n\t\t\t\tlastIndexE = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn lastIndexE;\n}\nint main()\n{\n\tcin >> N;\n\tA = new int[N];\n\tB = new int[N];\n\tQueueElem orig(N);\n\tfor(int i=0; i<n; i++)\n\t\tcin >>A[i];\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tcin >>B[i];\n\t\torig.addElem(B[i]);\n\t}\n\t\/\/\/\/\/\/\/\/\/\/\/\n\tW.push(orig);\n\tint count = N;\n\twhile (count--)\n\t{\n\t\tQueueElem qe = W.front();\n\t\tW.pop();\n\t\tint posM = findLastOnePos(qe);\n\t\tcout << qe.elements[posM];\n\t\tif( count > 0)\n\t\t\tcout << \" \";\n\t\tif(posM > 0)\n\t\t{\n\t\t\t\/\/\u6709\u5de6\u4fa7\u5143\u7d20\n\t\t\tQueueElem L(posM);\n\t\t\t\/\/\u5de6\u4fa7\u5143\u7d20\u7ec4\u6210\u5217\u538b\u6808\uff0c\u52a1\u5fc5\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff01\n\t\t\tfor(int i=0; i<posM; i++)\n\t\t\t\tL.addElem( qe.elements[i]);\n\t\t\tW.push(L);\n\t\t}\n\t\tif(qe.curSize - posM  > 1)\n\t\t{\n\t\t\t\/\/\u6709\u53f3\u4fa7\u5143\u7d20\n\t\t\tQueueElem R(qe.curSize - posM -1);\n\t\t\t\/\/\u53f3\u4fa7\u5143\u7d20\u7ec4\u6210\u5217\u538b\u6808\uff0c\u52a1\u5fc5\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff01\n\t\t\tfor(int i=posM +1; i<qe.curSize; i++)\n\t\t\t\tR.addElem( qe.elements[i] );\n\t\t\tW.push(R);\n\t\t}\n\t}\n\treturn 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[84],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-study","category-technical","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=114"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}