1099 Build A Binary Search Tree [二叉搜索树/中序、层次遍历]

先用中序确定节点的值,再用层次遍历输出即可。

写的时候思维江化,一开始用指针建树。。。

#include <bits/stdc++.h>
using namespace std;
#define maxn 105

struct Node
{
    int index,left,right;
}node[maxn];
int n,a[maxn],tree[maxn];
vector<int> ve[maxn],in,post;
void inorder(int p)
{
    if(node[p].left!=-1)
        inorder(node[p].left);
    in.push_back(node[p].index);
    if(node[p].right!=-1)
        inorder(node[p].right);
}
void bfs()
{
    cout<<tree[0];
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        if(t>0)
            cout<<" "<<tree[t];
        if(node[t].left!=-1)
            q.push(node[t].left);
        if(node[t].right!=-1)
            q.push(node[t].right);
    }
    cout<<endl;
}
int main()
{
    int l,r;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        node[i].index=i;
        node[i].left=l;
        node[i].right=r;
    }
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    inorder(0);
    for(int i=0;i<n;i++)
        tree[in[i]]=a[i];
    bfs();
    return 0;
}

原文地址:https://www.cnblogs.com/FTA-Macro/p/11443516.html

时间: 2024-09-27 02:26:37

1099 Build A Binary Search Tree [二叉搜索树/中序、层次遍历]的相关文章

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

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义. 不过如果是保存每层递归内的信息,就需要传参数. 本题显然是需要全局参数 /** * Definition for a binary tree node. * public class TreeNode { * int

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 甲级 1099 Build A Binary Search Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 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 th

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