二叉查找树——A1099.Build a Binary Search Tree(30) 非完全二叉查找树,但是给出了左右孩子节点,利用左右孩子节点构建二叉查找树,与上一题形成对比

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int maxn = 110;
struct Node{
    int data;
    int lchild;
    int rchild;
}Node[maxn];
int n,in[maxn],num = 0;
void inOrder(int root){
    if(root == -1){
        return;
    }
    inOrder(Node[root].lchild);
    Node[root].data = in[num++];
    inOrder(Node[root].rchild);
}
void BFS(int root){
    queue<int> q;
    q.push(root);
    num = 0;
    while(!q.empty()){
        int now = q.front();
        q.pop();
        printf("%d",Node[now].data);
        num++;
        if(num < n){
            printf(" ");
        }
        if(Node[now].lchild != -1){
            q.push(Node[now].lchild);
        }
        if(Node[now].rchild != -1){
            q.push(Node[now].rchild);
        }
    }
}
int main(){
    scanf("%d",&n);
    int temp;
    for(int i =0;i<n;++i){
        scanf("%d%d",&Node[i].lchild,&Node[i].rchild);
    }
    for(int i=0;i<n;++i){
        scanf("%d",&temp);
        in[i] = temp;
    }
    sort(in,in+n);
    inOrder(0);
    BFS(0);
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/JasonPeng1/p/12240209.html

时间: 2024-08-30 07:46:07

二叉查找树——A1099.Build a Binary Search Tree(30) 非完全二叉查找树,但是给出了左右孩子节点,利用左右孩子节点构建二叉查找树,与上一题形成对比的相关文章

pat1099. 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

1099. Build A Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1099. Build A Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制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

PAT 1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) 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 nod

1099. Build A Binary Search Tree (30)

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 greate

PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树BST]

题目 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 gre

PAT Advanced 1099 Build A Binary Search Tree (30分)

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 greate

【PAT甲级】1099 Build A Binary Search Tree (30 分)

题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 pair<int,int>a[107]; 5 int b[107]; 6 int cnt=0; 7 int ans[107]

PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int

PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子.中序遍历的顺序正好是序列从小到大的顺序,因此中序遍历的时候顺便赋值就可以了,最后层次遍历输出. 思路一:中序遍历的时候赋值 #include <iostream> #include <cstdio> #include <algorithm> #include <str