{"id":521,"date":"2013-09-04T18:10:19","date_gmt":"2013-09-04T10:10:19","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=521"},"modified":"2013-09-04T18:10:19","modified_gmt":"2013-09-04T10:10:19","slug":"1064-complete-binary-search-tree-30","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=521","title":{"rendered":"1064. Complete Binary Search Tree (30)"},"content":{"rendered":"<div style=\"text-align: right;\">\u65f6\u95f4\u9650\u5236<\/div>\n<div style=\"text-align: right;\">50 ms<\/div>\n<div style=\"text-align: right;\">\n<div>\u5185\u5b58\u9650\u5236<\/div>\n<div>32000 kB<\/div>\n<\/div>\n<div>\n<div style=\"text-align: right;\">\u4ee3\u7801\u957f\u5ea6\u9650\u5236<\/div>\n<div style=\"text-align: right;\">16000 B<\/div>\n<div><\/div>\n<\/div>\n<p>A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:<\/p>\n<ul>\n<li>The left subtree of a node contains only nodes with keys less than the node&#8217;s key.<\/li>\n<li>The right subtree of a node contains only nodes with keys greater than or equal to the node&#8217;s key.<\/li>\n<li>Both the left and right subtrees must also be binary search trees.<\/li>\n<\/ul>\n<p>A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.<br \/>\nNow given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. For each case, the first line contains a positive integer N (&lt;=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>10\n1 2 3 4 5 6 7 8 9 0<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>6 3 8 1 5 7 9 0 2 4<\/pre>\n<p>========================================<br \/>\n\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f&#8230;\u5b8c\u5168\u4e8c\u53c9\u641c\u7d22\u6811<br \/>\n\u4e24\u4e2a\u7279\u5f81\uff1a\u4e8c\u53c9\u641c\u7d22\u6811+\u5b8c\u5168\u4e8c\u53c9\u6811<br \/>\n\u540e\u9762\u7684\u4ee3\u7801\u91cc\u9762\u53ef\u80fd\u6709\u4e00\u70b9\u5197\u4f59\u7684\u5730\u65b9\uff0c\u5c31\u662f\u90a3\u4e2aif\u91cc\u9762\u7684\uff0c\u4e0d\u8fc7\u8fd0\u884c\u7ed3\u679c\u4e0d\u5f71\u54cd\uff0c\u5c31\u6ca1\u53bb\u6539\u4e86\u3002<br \/>\n\u6211\u60f3\u7684\u529e\u6cd5\u662f\u81ea\u9876\u5411\u4e0b\u7684\uff0c\u4ece\u4e00\u5806\u6392\u597d\u5e8f\u7684\u6570\u4e2d\u63ea\u51fa\u90a3\u4e2a\u201c\u4e2d\u95f4\u7684\u201d\uff0c\u7136\u540e\u5206\u4e3a\u4e24\u68f5\u5b50\u6811\u505a\uff0c\u5982\u6b64\u8fed\u4ee3(\u5177\u4f53\u8f93\u51fa\u7528&lt;queue&gt;\u7684\u6570\u7ec4\u4e00\u5c42\u5c42\u5f04\u5c31\u884c\u4e86\uff0c\u8fd9\u8fb9\u61d2\u5f97\u5199)\uff1a<\/p>\n<blockquote><p>function foo:<br \/>\n\u672c\u884c\u5b8c\u6574\u7684\u5e94\u6709m = log2(N)\u4e2a<br \/>\n\u672c\u884c\u591a\u51fa\u7684\u6709r = N &#8211; m\u4e2a \uff08\u5c31\u662f\u5b8c\u5168\u6811\u7684\u6700\u5e95\u4e0b\u4e00\u884c\u591a\u4f59\u7684\u8282\u70b9\u6570\uff09<br \/>\nif r==0:<br \/>\n\u592a\u5b8c\u7f8e\u4e86..\u5c06\u4e2d\u95f4\u7684\u6570\u627e\u51fa\uff0c\u653e\u5165\u8f93\u51fa\u961f\u5217\u4e2d<br \/>\nelse:<br \/>\n\u6700\u540e\u4e00\u6392\u586b\u6ee1\u7684\u8bdd\u4f1a\u6709k=2^m\u4e2a\u5143\u7d20<br \/>\n\u90a3\u4e48\u5de6\u53f3\u8fb9\u5206\u5f00\u7684\u8bdd\uff0c\u5404\u6709k\/2\u4e2a<br \/>\n\u8fd9\u6837\u5c31\u80fd\u7b97\u51fa\u5de6\u8fb9\u53f3\u8fb9\u5e94\u8be5\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u201c\u591a\u4f59\u201d\uff0c\u53bb\u9664\u5de6\u53f3\u8fb9\u591a\u4f59\u7684\u5269\u4e0b\u5c31\u662f\u771f\u6b63\u7684\u957f\u5ea6<br \/>\n\u53bb\u9664\u591a\u4f59\u7684\u5143\u7d20\uff0c\u6839\u636e\u957f\u5ea6\u627e\u51fa\u4e2d\u95f4\u7684\u5143\u7d20\uff0c\u653e\u5165\u8f93\u51fa\u961f\u5217<br \/>\n\u9012\u5f52\u63a5\u7740\u7b97<\/p><\/blockquote>\n<p>=======================================================================<br \/>\n<code lang=\"c++\"><br \/>\n#include <stdio.h><br \/>\n#include <stdlib.h>     \/* qsort *\/<br \/>\n#include <math.h><br \/>\n#include <queue><br \/>\nusing namespace std;<br \/>\nint compare(const void *a, const void *b)<br \/>\n{<br \/>\n    return ( *(int*)a - *(int*)b);<br \/>\n}<br \/>\nqueue<int> *results;<br \/>\nvoid run(int* srcAry, int fromIndex, int toIndex, int level)<br \/>\n{<br \/>\n    int length = toIndex - fromIndex + 1;<br \/>\n    if(length < 1)\n        return;\n    if(length == 1)\n    { \/\/\u5230\u5e95\u4e86\n        \/\/printf(\"%d \", srcAry[fromIndex]);\n        results[level].push(srcAry[fromIndex]);\n        return;\n    }\n    int m = log((double)length)\/log((double)2);\n    int r = length - pow(2.0f,m) + 1;\n    if(r == 0)\n    {\n        int midIndex = fromIndex + length\/2;\n        \/\/printf(\"%d \", srcAry[midIndex]);\n        results[level].push(srcAry[midIndex]);\n        run(srcAry, fromIndex, midIndex-1, level-1);\n        run(srcAry, midIndex+1, toIndex, level-1);\n    }\n    else\n    {\n        int k = pow(2.0f, m);\n        int left = k\/2, right = k\/2;\n        if(r <= left)\n        {\n            \/\/\u53ea\u6709\u5de6\u8fb9\u6709\n            left = r;\n            right = 0;\n        }\n        else\n        {\n            \/\/\u53f3\u8fb9\u4e5f\u6709\n            right = r - left;\n        }\n        int restLength = length - r;\n        int midIndex = fromIndex + left + restLength\/2;\n        \/\/printf(\"%d \", srcAry[midIndex]);\n        results[level].push(srcAry[midIndex]);\n        run(srcAry, fromIndex, midIndex -1, level-1);\n        run(srcAry, midIndex+1, toIndex, level-1);\n    }\n}\nint main()\n{\n    int N;\n    scanf(\"%d\", &#038;N);\n    int *ary = new int[N];\n    for(int i=0; i<n; i++)\n    {\n        scanf(\"%d\", &#038;ary[i]);\n    }\n    qsort(ary, N, sizeof(int), compare);\n    int level = ceil(log((double)N)\/log((double)2));\n    results = new queue<int>[level +1];<br \/>\n    run(ary, 0, N-1, level);<br \/>\n    int count = 1;<br \/>\n    level++;<br \/>\n    while (level--)<br \/>\n    {<br \/>\n        queue<int> q = results[level];<br \/>\n        while(!q.empty())<br \/>\n        {<br \/>\n            printf(\"%d\", q.front());<br \/>\n            if(count < N)\n                printf(\" \");\n            count ++;\n            q.pop();\n        }\n    }\n    return 0;\n}\n<\/code><br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236 50 ms \u5185\u5b58\u9650\u5236 32000 kB \u4ee3\u7801\u957f\u5ea6\u9650\u5236 16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node&#8217;s key. The right subtree of a node contains only nodes with keys greater than [&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],"tags":[84],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/521","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=521"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}