pat1191119. Pre- and Post-order Traversals
来源:互联网 发布:linux grep查找字符串 编辑:程序博客网 时间:2024/06/06 13:15
原创文章using namespace std;#include <iostream>#include <vector>void dfs(vector<int>&preorder, vector<int>&postorder, vector<int>&inorder,bool&flag, int l1, int r1, int l2, int r2){ if (l1 == r1) { inorder.push_back(preorder[l1]); return; } int index = -1; for (int i = l1 + 1; i <= r1; i++) { if (preorder[i] == postorder[r2 - 1]) { index = i; break; } } if(preorder[l1 + 1] == postorder[r2 - 1]) { flag = false; dfs(preorder, postorder, inorder, flag,l1+ 1, r1, l2,r2 - 1); inorder.push_back(preorder[l1]); }else { dfs(preorder, postorder, inorder, flag,l1+ 1, index - 1, l2,l2 + index - 1 - l1 - 1); inorder.push_back(preorder[l1]); dfs(preorder, postorder, inorder, flag, index, r1, r2 - 1 - r1 + index,r2 - 1); }}int main(){ int N; cin >> N; vector<int> pre(N,0); vector<int> post(N,0); for (int i = 0; i < N; i++) { cin >> pre[i]; } for (int i = 0; i < N; i++) { cin >> post[i]; } vector<int> inorder; bool flag = true; dfs(pre, post, inorder, flag, 0, N - 1, 0, N - 1); if (flag) { cout << "Yes" << endl; } else { cout << "No" << endl; } for (int i = 0; i < N; i++) { if (i == 0) { cout << inorder[i]; continue; } cout << " " << inorder[i]; } cout << endl; }
0 0
- pat1191119. Pre- and Post-order Traversals
- Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals
- PAT1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- PAT--1119. Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals
- (PAT)1119. Pre- and Post-order Traversals
- 【PAT】1119. Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals (30)
- php 基础
- 单链表的应用
- AdapterViewFlipper设置动画问题
- MySQL 使用 LOAD DATA 导入 csv 文件
- hdu5726 && hdu5869
- pat1191119. Pre- and Post-order Traversals
- 降低OpenWRT的Linux内核版本
- jquery该放在哪个文件夹啊。还有如何引入啊路径问题
- Yocto i.MX6 (TQIMX6) (03) : wifi网卡的相关工具wpa_supplicant与dhcp
- 【干货】5步搞定android混淆
- 12章 一T
- delphi三层架构
- android eclipsed下载 安装
- poj--3273 Monthly Expense(最小化最大值)