1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30)
時間限制 100 ms內存限制 65536 kB
代碼長度限制 16000 B
判題程序 Standard 作者 CHEN, Yue
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's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input: 9 1 6 2 3 -1 -1 -1 4 5 -1 -1 -1 7 -1 -1 8 -1 -1 73 45 11 58 82 25 67 38 42 Sample Output: 58 25 82 11 38 67 45 73 42
解析:
1,二叉樹的表示方法有三種,數組表示法,二叉樹結構數組表示法,鏈表結構表示法,本題用的是結構數組表示法.
2,PAT的題目還是很普通的,掌握幾種常用的數據結構設計方案,以及相應的算法,就可以搞定大部分題目.
3,先想幾個簡單的問題:
3.1 給定一個數子序列,構造二叉查找樹是不唯一的;
3.2 不僅是不唯一,而且是可以構造出任意形狀的二叉查找樹;
3.3 同一個形狀的二叉查找樹,會不會有多個,不會,可以從根結點往下考慮,首先根結點必須相同,否則兩棵樹對應的左右兩邊個數不會相等,用數學歸納法知,所有元素一一對應相等;
3.4 中序遍歷二叉查找樹出來的結果就是元素遞增排序,為什么?因為 {左子樹} ?< 根 < {右子樹}, 那么再展開子樹到葉子結點,再去掉{}后就是a1 < a2 < a3 < a4....這個樣子;
3.5 反過來,一棵樹中序遍歷展開后是升序,是否這棵樹是二叉查找樹, 答案是, 從根結點往下考慮,用數學歸納法,所以二叉查找樹還可定義,中序遍歷升序,則為二叉查找樹;
3.6 根據上面推出來的二叉查找樹等價定義, 將二叉樹的結點按中序遍歷展開,把給定的元素按生序排列, 對應填入到結點中, 即可構造出一棵二叉查找樹.
代碼如下:
/*************************************************************************> File Name: 1099.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Sun 21 May 2017 07:45:35 AM CST************************************************************************/#include <stdio.h> #include <stdlib.h>int idx = 0; int cmp(const void *a, const void *b) {return *(int *)a - *(int *)b; }typedef struct {int data;int left;int right; } node;void InorderBuildTree(node *treeArr, int *elements, int root) {if (root == -1)return;InorderBuildTree(treeArr, elements, treeArr[root].left);treeArr[root].data = elements[idx];idx++;InorderBuildTree(treeArr, elements, treeArr[root].right); }void printfLevelOrder(node *treeArr, int root) { node qu[1024]; int front, rear; front = rear = -1; if (root == -1) return; rear++; qu[rear].data = treeArr[root].data; qu[rear].left = treeArr[root].left; qu[rear].right = treeArr[root].right; while (rear != front) { if (front != -1) printf(" "); printf("%d", qu[++front].data); if (qu[front].left != -1) { rear++; qu[rear].data = treeArr[qu[front].left].data; qu[rear].left = treeArr[qu[front].left].left; qu[rear].right = treeArr[qu[front].left].right; } if (qu[front].right != -1) { rear++; qu[rear].data = treeArr[qu[front].right].data; qu[rear].left = treeArr[qu[front].right].left; qu[rear].right = treeArr[qu[front].right].right; } } printf("\n"); } int main() {int N;node treeArr[128];int i;int elements[128];scanf("%d", &N);for (i = 0; i < N; i++)scanf("%d%d", &treeArr[i].left, &treeArr[i].right);for (i = 0; i < N; i++)scanf("%d", elements+i);qsort(elements, N, sizeof(elements[0]), cmp);InorderBuildTree(treeArr, elements, 0);printfLevelOrder(treeArr, 0);return 0; }代碼說明:上面的代碼中在遞歸構造二叉查找樹函數void InorderBuildTree(node *treeArr, int *elements, int root)使用了idx這個全局變量,以后再改進這段代碼,爭取使這個函數保持獨立性.
總結
以上是生活随笔為你收集整理的1099. Build A Binary Search Tree (30)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js高级编程_这位设计师用Process
- 下一篇: 商务计算机的购置方案,电子采购方案的实施