
树
QQN1996
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
1127 ZigZagging on a Tree(30 分)【树的遍历】
题意:已知中序遍历和后序遍历,建树,输出层序遍历,输出规则为:从左到右,再从右到左,交替输出 #include <bits/stdc++.h> using namespace std; #define INF 0x7FFFFF int n; int in[31],po[31]; typedef struct node { int val; struct node...原创 2018-09-04 19:12:24 · 693 阅读 · 0 评论 -
1138 Postorder Traversal(25 分)【树的遍历】
题意:已知前序遍历和中序遍历,输出后序遍历 //超时的时候可以试试将cin、cout换成scanf、printf #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *lchild,*rchild; }; int n; int pre[50005],...原创 2018-09-04 19:17:27 · 323 阅读 · 0 评论 -
1115 Counting Nodes in a BST(30 分)【二叉搜索树】
计算一颗二叉搜索树最底两层的节点数,并且求和。 首先根据输入的数值建立二叉搜索树。然后用dfs/bfs的方法计算最后最底两层的节点数,最后按照题目要求输出答案。 dfs: #include <bits/stdc++.h> using namespace std; #define INF 0x7FFFFF int n; struct node { int dat...原创 2018-09-04 19:22:50 · 343 阅读 · 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 · 229 阅读 · 0 评论 -
1143 Lowest Common Ancestor(30 分)【最近公共祖先】
二叉搜索树的建树和寻找最近公共祖先(题目给出了BST的前序遍历,而前序遍历升序排列就是BST的中序遍历了) #include <bits/stdc++.h> using namespace std; typedef struct node *Node; typedef struct node{ int val; Node l,r; }node; Node T; ...原创 2018-08-30 10:36:00 · 300 阅读 · 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 · 317 阅读 · 0 评论