二叉树の学习
蓝
这个作者很懒,什么都没留下…
展开
-
7-59 还原二叉树 (25分)
这道题相当于给出先序和中序求后序遍历的题差不多,只需要记录一下depth就行了。#include <iostream>#include <algorithm>#include <cstring>#include <stack>using namespace std;const int maxn = 60;char pre[maxn],in[maxn];int n,mx=1;void dfs(int root, int start, int .原创 2020-09-26 16:56:08 · 252 阅读 · 0 评论 -
7-37 是否同一棵二叉搜索树 (25分)
这道题需要首先建立一个二叉搜索树,然后再一一对比。#include<bits/stdc++.h>#define fi first#define se second#define pb pusb_back#define SZ(x) (ll)x.size()#define rep(i,a,b) for(ll i=(a);i<=(b);++i)#define per(i,a,b) for(ll i=(a);i>=(b);--i)#define mem(a,b) memse.原创 2020-09-10 17:54:30 · 220 阅读 · 0 评论 -
7-35 List Leaves (25分)
这道题的意思是,给出你每个结点(结点从0-n-1)的左右孩子,然后让你从上到下,从左到右输出叶子结点。这道题的思路是用队列做,首先用结构体存储结点的左右孩子,以便后面队列找叶结点。首先找到根节点(找的方法是创建一个标记数组,每次输入结点时,vis[结点]++,当某个结点vis数组存储的值时0时,就是根节点),然后从根节点开始左右搜索,依次找到叶结点。代码如下:#include <iostream>#include <algorithm>#include <cstri.原创 2020-09-10 13:05:14 · 210 阅读 · 0 评论 -
7-15 树的遍历 (25分)
这个题就是转层序,在函数后面加一个index统计各个点的位置就可以了,跟玩转二叉树基本上一样。代码:#include <iostream>#include <algorithm>#include <cstring>#include <sstream>using namespace std;const int N = 50;int in[N],post[N],level[100000];int cnt = 0;int n;void dfs.原创 2020-09-10 11:40:24 · 295 阅读 · 0 评论 -
二叉树の学习之最近公共祖先
最近做了二叉树类型的题目,做了洛谷的P3884 [JLOI2009]二叉树问题,学习了一种新的算法(倍增),最近公共祖先题目链接:[P3884二叉树问题](https://www.luogu.com.cn/problem/P3884)还有另外一道题:P3379 【模板】最近公共祖先(LCA)感兴趣的可以看一下。上代码:#include<bits/stdc++.h>#def...原创 2020-04-22 10:21:22 · 114 阅读 · 0 评论 -
7-10 玩转二叉树 (25分)
写这个题是借鉴了学长的思路,在反转的时候只需要在遍历左半部分和右半部分的时候将结点转换一下位置就可以了,需要注意的是,存储层序数组的结点需要开大一些,否则最后一个测试点过不去,最后就是输出的时候需要借助一个cnt输出答案。代码如下:#include <iostream>#include <algorithm>#include <cstring>#include <sstream>using namespace std;const int N =.原创 2020-09-10 10:11:48 · 2441 阅读 · 0 评论 -
7-7 根据后序和中序遍历输出先序遍历 (25分)
题目模板如下:#include <iostream>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int N = 40;int in[N],post[N],pre[N];int n;int index;void dfs(int root, int start, int end){ if(start > end) re.原创 2020-09-09 20:58:16 · 482 阅读 · 1 评论