• 1099.Build A Binary Search Tree [指定结构的二叉搜索树层序遍历]


    我又又又想不出来了,一开始想的和那个完全二叉搜索树一样,中序遍历输出层序,结果他指定了结构,然后我就没转过来、、、

    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
    

    	#include<iostream>
    	#include<vector>
    	#include<algorithm>
    	using namespace std;
    	struct node {
    		int data,l, r, index, layer;
    	}a[100];
    	int n, b[100]; int cnt = 0;
    	bool cmp(node x,node y) {
    		if (x.layer != y.layer) return x.layer < y.layer;
    		else return x.index < y.index;
    	}
    	void getlevel(int root,int index,int layer) {
    			if (a[root].l != -1)getlevel(a[root].l, index * 2 + 1, layer + 1);
    			a[root] = { b[cnt++],a[root].l,a[root].r,index,layer };
    			if (a[root].r != -1)getlevel(a[root].r, index * 2 + 2, layer + 1);
    	}
    	int main() {
    		cin >> n;
    		for (int i = 0; i < n; i++)
    			cin >> a[i].l >> a[i].r;
    		for (int i = 0; i < n; i++)
    			cin >> b[i];
    		sort(b, b + n);
    		getlevel(0, 0, 1);
    		sort(a, a + n, cmp);
    		cout << a[0].data;
    		for (int i = 1; i < n; i++)
    			cout << " " << a[i].data;
    
    
    	}
    
  • 相关阅读:
    HTML超链接a标签
    ANR与Crash
    学习笔记:oracle学习一:oracle11g体系结构之物理存储结构
    学习笔记:oracle学习一:oracle11g体系结构之服务器结构、数据字典
    关于sql server全文检索的一些资料收集2
    关于sql server全文检索的一些资料收集1
    关于sql server全文检索的一些资料收集2
    做日文编码网页解决乱码的经验。(仇日派别砍我~~)
    如何监视sql server连接池
    做全文检索的小心得
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109980.html
Copyright © 2020-2023  润新知