当前位置:网站首页>First in the binary tree
First in the binary tree
2022-04-23 03:07:00 【Learning KL & TK】
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
char val;
TreeNode* l;
TreeNode* r;
};
// TODO( Recursion to the current layer , mid by Currently, the order nodes in this tree are constructed ,hou The current post order traverses the node )
// TODO len The number of nodes currently building this tree
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 Build a left subtree
// todo 1. xian + 1 ~ xian + index Priority range
// todo 2. mid + 0 ~ mid + index - 1
root->l = build(xian + 1, mid, index, high + 1);
// todo Build right subtree
// todo 1. xian + index + 1 ~ xian + index + 1 + (len - index - 1) - 1 Range of right subtree in line order
// 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;
}
版权声明
本文为[Learning KL & TK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230303365478.html
边栏推荐
- .NET7之MiniAPI(特别篇):.NET7 Preview3
- C语言实现通讯录----(静态版本)
- Using stack to solve the problem of "mini parser"
- Response processing of openfeign
- MYSQL05_ Ordr by sorting, limit grouping, group by grouping
- 微软是如何解决 PC 端程序多开问题的
- AspNetCore配置多环境log4net配置文件
- The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
- Introduction and use of openfeign component
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
猜你喜欢

类似Jira的十大项目管理软件

Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!

ASP. Net 6 middleware series - Custom middleware classes

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)

Small companies don't make formal offers

最通俗易懂的依赖注入与控制反转

svg标签中利用<polygon/>循环数组绘制多边形

【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持

Source generator actual combat

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
随机推荐
Vs code setting line feed
C语言实现通讯录----(静态版本)
Cherno_ Game engine series tutorial (5): 101~
Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
HLS / chisel practice CORDIC high performance computing complex square root
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
C read / write binary file
Source Generator实战
Use of slice grammar sugar in C #
svg标签中利用<polygon/>循环数组绘制多边形
求二叉树的叶子结点个数
Distributed system services
Small companies don't make formal offers
Laravel new route file
How to count the number of all files in a directory under win10 system
TP5 multi conditional where query (using PHP variables)
FileNotFoundError: [Errno 2] No such file or directory
Detailed log display of openfeign call
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
最通俗易懂的依赖注入之服务容器与作用域