[PAT]1099 Build A Binary Search Tree (30 分)(只得22分的原因)

13 篇文章 0 订阅

一、题目:

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 Ndistinct 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

题目大意是:

给出二叉搜索树的结构,和节点的权值(乱序),求出二叉搜索树的层次遍历。

解析:

我第一次做事按照:建树、给树节点赋值、层次遍历。这个步骤做的,使用的链表,但是只得22分,看了别人的代码意识到自己的问题所在。如果你也没得满分可能原因如下:

样例输入第一个数n为节点个数,然后跟着n行输入,分别代表第0、1、2、3、、、n-1号节点的左右子节点号。例如:

9    //表示搜索树有9个节点
1 6    //表示第0号节点左右孩子分别为为第1和第6号节点
2 3    //表示第1号节点左右孩子分别为为第2和第3号节点
-1 -1    //表示第2号节点没有左右孩子
-1 4    //表示第3号节点没有左孩子右孩子为第4号节点
5 -1    //表示第4号节点没有右孩子,左孩子为第5号节点
-1 -1    //表示第5号节点没有左右孩子
7 -1    //表示第6号节点左孩子为第7号节点,没有右孩子
-1 8    //表示第7号节点没有左孩子,右孩子为第8号节点
-1 -1    //表示第8号节点没有左右孩子
73 45 11 58 82 25 67 38 42    //表示第0号到第8号节点的值分别为73 45 11....

而我第一次做的时候是按照n行输入中非-1值得顺序建树的,我当时使用一个队列第一次入根节点push(0)、然后根据n行输入如果不是-1先入右节点push(right)、再入左节点push(left)。每次都对队列的队首进行赋值,这样做只能得22分。最后按照网上其他人的代码做的才AC。

本题目AC的解题思路为:根据输入建树。对节点值按从小到大排序,所得到的的序列即为BST的中序遍历,据此对节点进行赋值。最后再层次遍历。

下面给出AC代码:

#include <bits/stdc++.h>

using namespace std;

struct node {
    int key;
    int val;
    int leftKey;
    int rightKey;
    node* left;
    node* right;
    node(int key, int val=-1, node* left=NULL, node* right=NULL) {
        this->key = key;
        this->val = val;
        this->left = left;
        this->right = right;
    }
};

node* root;
vector<int> seque;
vector<node*> nodes;

//中序遍历
void inOrder(node* root) {
    if(root==NULL) {
        return;
    }

    inOrder(root->left);


    root->val = seque[0];
    seque.erase(seque.begin());

    inOrder(root->right);


}

int main() {

    int n;
    cin>>n;

    nodes.resize(n);

    //先申请n个结点,并且记录各个结点的左右孩子节点key
    for(int i=0; i<n; i++) {
        int left, right;
        cin>>left>>right;
        node* temp = new node(i);
        temp->leftKey = left;
        temp->rightKey = right;
        nodes[i] = temp;
    }

    //对n个节点的左右孩子进行赋值
    for(int i=0; i<n; i++) {
        if(nodes[i]->leftKey != -1) {
            nodes[i]->left = nodes[nodes[i]->leftKey];
        }
        if(nodes[i]->rightKey != -1) {
            nodes[i]->right = nodes[nodes[i]->rightKey];
        }
    }

    //现在整个树的结构已经建立,但是节点的val还没有赋值
    root = nodes[0];

    for(int i=0; i<n; i++) {
        int a;
        cin>>a;
        seque.push_back(a);
    }


    sort(seque.begin(), seque.end());

    //对树中的结点进行赋val
    inOrder(root);


    //层次遍历
    queue<node*> q;
    q.push(root);

    vector<int > ans;
    while(!q.empty()){
        ans.push_back(q.front()->val);
        if(q.front()->left!=NULL){
            q.push(q.front()->left);
        }
        if(q.front()->right!=NULL){
            q.push(q.front()->right);
        }

        q.pop();
    }

    for(int i=0; i<ans.size()-1; i++){
        cout<<ans[i]<<" ";
    }
    cout<<ans.back()<<endl;



    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值