标签:bfs style can include 节点 ons oid const 查找树
#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; }
二叉查找树——A1099.Build a Binary Search Tree(30) 非完全二叉查找树,但是给出了左右孩子节点,利用左右孩子节点构建二叉查找树,与上一题形成对比
标签:bfs style can include 节点 ons oid const 查找树
原文地址:https://www.cnblogs.com/JasonPeng1/p/12240209.html