当前位置:网站首页>先中二叉建树
先中二叉建树
2022-04-23 03:03:00 【学习kl&tk】
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
char val;
TreeNode* l;
TreeNode* r;
};
// TODO(递归到当前这一层, mid 为 当前构建这颗树中序节点 ,hou 当前的后序遍历节点)
// TODO len 当前构建这颗树的节点个数
int ans = 0;
TreeNode* build(char xian[], char mid[], int len, int high) {
if (len == 0) return NULL;
ans = max(ans, high);
char tmp = xian[0];
int index = -1;
for(int i = 0; i < len; i++) {
if (tmp == mid[i]) {
index = i ;
break;
}
}
TreeNode* root = new TreeNode();
root->val = tmp;
// todo 建左子树
// todo 1. xian + 1 ~ xian + index 先序范围
// todo 2. mid + 0 ~ mid + index - 1
root->l = build(xian + 1, mid, index, high + 1);
// todo 建右子树
// todo 1. xian + index + 1 ~ xian + index + 1 + (len - index - 1) - 1 线序中右子树的范围
// todo 2. mid + index + 1 ~
root->r = build(xian + index + 1, mid + index + 1, len - index - 1, high + 1);
return root;
}
char a[55], b[55];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
TreeNode* root = build(a, b, n, 1);
cout << ans << endl;
return 0;
}
版权声明
本文为[学习kl&tk]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53013914/article/details/124325721
边栏推荐
- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- Publish to NPM?
- Openfeign details show
- Winsock programming interface experiment: Ping
- AOT和单文件发布对程序性能的影响
- Liunx foundation - zabbix5 0 monitoring system installation and deployment
- 如果通过 C# 实现对象的深复制 ?
- Response processing of openfeign
- Classification of technology selection (2022)
- C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
猜你喜欢

Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

Configuring Apache Web services for servers such as Tianyi cloud

Cherno_ Game engine series tutorial (5): 101~

Opencv fills the rectangle with a transparent color

Blazor University (11)组件 — 替换子组件的属性

ASP.NET 6 中间件系列 - 执行顺序

最通俗易懂的依赖注入之服务容器与作用域

Recursion - outputs continuously increasing numbers
![Introduction to ACM [inclusion exclusion theorem]](/img/3a/9bc2a972d7587aab51fceb8cd2b9bd.png)
Introduction to ACM [inclusion exclusion theorem]
随机推荐
Summary of interface automation interview questions for software testing
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Chapter VI project information management system summary
最通俗易懂的依赖注入之服务容器与作用域
How to write the expected salary on your resume to double your salary during the interview?
Introduction to ACM [TSP problem]
tf. keras. layers. MaxPooling? D function
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Close the computer port
Array and collection types passed by openfeign parameters
Openfeign timeout setting
Numpy stack function
Restart redis
AOT和单文件发布对程序性能的影响
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
MYSQL_ From mastery to abandonment
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Vs code setting line feed
[Euler plan question 13] sum of large numbers
TP5 multi conditional where query (using PHP variables)