DFS
战南城
学习笔记,不喜勿喷!
展开
-
PAT A1115(二叉树的遍历,使用BFS或DFS解题)
使用BFS解题(那啥,使用DFS解题是最简单的)#include<iostream>#include<queue>using namespace std;int n, hashTable[1010] = {0}, maxLayer = -1;struct node{ int data, layer; node *left, *right;};void insert(node* &root, int x) { if(root == NULL) { .原创 2021-03-11 17:32:00 · 74 阅读 · 0 评论 -
PAT A1151(最近公共祖先)
建树, 该算法需要900多毫秒,在超时的边缘徘徊。#include<iostream>#include<map>using namespace std;const int maxn = 10005;int m, n, in[maxn], pre[maxn];struct node{ int par, layer;};map<int, node> arr;void create(int inL, int inR, int preRoot, node.原创 2021-03-05 17:15:10 · 52 阅读 · 0 评论 -
A1155(易理解系列)
#include<iostream>#include<vector>using namespace std;int n, level[1010];vector<int> path;void DFS(int x) { path.push_back(level[x]); if(2 * x + 1 <= n) DFS(2 * x + 1); if(2 * x <= n) DFS(2 * x); if(2 * x > n) { for(i原创 2021-03-04 14:46:29 · 29 阅读 · 0 评论