bfs
QQN1996
这个作者很懒,什么都没留下…
展开
-
1127 ZigZagging on a Tree(30 分)【树的遍历】
题意:已知中序遍历和后序遍历,建树,输出层序遍历,输出规则为:从左到右,再从右到左,交替输出#include <bits/stdc++.h>using namespace std;#define INF 0x7FFFFFint n;int in[31],po[31];typedef struct node{ int val; struct node...原创 2018-09-04 19:12:24 · 637 阅读 · 0 评论 -
1115 Counting Nodes in a BST(30 分)【二叉搜索树】
计算一颗二叉搜索树最底两层的节点数,并且求和。首先根据输入的数值建立二叉搜索树。然后用dfs/bfs的方法计算最后最底两层的节点数,最后按照题目要求输出答案。 dfs:#include <bits/stdc++.h>using namespace std;#define INF 0x7FFFFFint n;struct node{ int dat...原创 2018-09-04 19:22:50 · 281 阅读 · 1 评论 -
1004 Counting Leaves(30 分)【dfs/bfs】
题意:给你一个家谱树,找到没有孩子的人,即寻找每层的叶子结点数方法一:用dfs。采用vector数组作为树的存储结构,将树结点逐一输入到vector 中,每一个元素存储了该结点的子孩子信息,自动向下探索子孩子,直到vector[i].size()==0表明该结点的子孩子为零,即叶子结点。 #include <bits/stdc++.h>using namespace std...原创 2018-08-30 10:42:29 · 186 阅读 · 0 评论 -
1020 Tree Traversals(25 分)【树的遍历】
题意:已知后序遍历和中序遍历,构造树,然后输出层序遍历(用bfs)#include <bits/stdc++.h>using namespace std;const int maxn=35;int n;int post[maxn];int in[maxn];typedef struct node{ int val; struct node *lchi...原创 2018-08-30 10:57:01 · 267 阅读 · 0 评论