当前位置:网站首页>Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
2022-07-02 10:59:00 【Morgannr】
Ergodic problem
Title Description
We are all familiar with the preorder of binary trees 、 Middle preface 、 After the sequence traversal , Such problems are often raised in data structures : We know the preorder and inorder traversal of a binary tree , Find its postorder traversal , Corresponding , Given the post order traversal and middle order traversal sequences of a binary tree, you can also find its pre order traversal . However, given the preorder and postorder traversal of a binary tree , But you can't be sure of the middle order traversal sequence , Consider several binary trees as shown in the figure below :

All these binary trees have the same preorder traversal and postorder traversal , But the middle order traversal is different .
Input format
transport A There are two lines of data , The first line represents the preorder traversal result of the binary tree s1, The second line represents the post order traversal result of the binary tree s2.
Output format
Output the total number of possible middle order traversal sequences , The result does not exceed the number of long integers .
Examples #1
The sample input #1
abc
cba
Sample output #1
4
Tips
No prompting
The question :
Given a binary tree front 、 After the sequence traversal , You can get Middle order traversal is not unique , Get and output The number of ordered traversals in a binary tree .
Ideas :
The requirement of the title is How many kinds of results are there for finding the middle order traversal
Binary tree The situation of child nodes is no different from 4 Kind of :
- Left and right subtrees 、 The left subtree 、 Right subtree 、 empty ,
stay this 4 In this case A node There are left and right subtrees and Node subtree is empty Under the circumstances The middle order traversal is fixed ,
therefore Only in the case of one node, there will be two cases of intermediate order traversal results , So the discussion of the problem can be transformed into :
- Through the preface and postscript, we can know
BinTreeHow many nodes in have only one subtree
about There is only one subtree The node of ( namely a There is only one subtree b), We can find it in In the preamble and postscript The law of is :
- stay
PreOrderinaThe successor isb, stayPostOrderinaThe precursor to this isb
then Traverse the pre order and post order ,cnt Indicates how many nodes meet the conditions , Every such node signify BinTree There will be more 2 Seed result ( Left or Right ) So the end result is 2 Of cnt Power ( Multiplication principle )
Time complexity :
O ( n 2 ) O(n ^ 2) O(n2)
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
string pre, post;
int solve()
{
int cnt = 0;
for (int i = 0; i < pre.size() - 1; ++i)
{
for (int j = 1; j < post.size(); ++j)
{
auto a = pre[i], b = post[j];
if (a == b && pre[i + 1] == post[j - 1]) ++cnt;
}
}
return (1 << cnt);
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> pre >> post;
cout << solve() << '\n';
}
return 0;
}
边栏推荐
- What are the popular frameworks for swoole in 2022?
- [visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
- 如何用list组件实现tabbar标题栏
- How to get the password of cpolar?
- Common methods of JS array
- Mysql database remote access permission settings
- 如何使用IDE自动签名调试鸿蒙应用
- UWA report uses tips. Did you get it? (the fourth bullet)
- Hdu1236 ranking (structure Sorting)
- 二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
猜你喜欢

UVM learning - object attribute of UVM phase

HDU1236 排名(结构体排序)
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

(5) Gear control setting of APA scene construction
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

Operator-1 first acquaintance with operator

618再次霸榜的秘密何在?耐克最新财报给出答案

1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
![2.hacking-lab脚本关[详细writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)
2.hacking-lab脚本关[详细writeup]

Shell programming 01_ Shell foundation
随机推荐
Learn open62541 -- [66] UA_ Generation method of string
Nodejs+express+mysql simple blog building
LabVIEW为什么浮点数会丢失精度
PCL extracts a subset from a point cloud
Hdu1234 door opener and door closer (water question)
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
[AI application] Hikvision ivms-4200 software installation
2022-06-17
In the face of uncertainty, the role of supply chain
主键策略问题
Jsp webshell Free from killing - The Foundation of JSP
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
P1055 [noip2008 popularization group] ISBN number
What are the popular frameworks for swoole in 2022?
二叉树专题--AcWing 1589. 构建二叉搜索树
12. Process synchronization and semaphore
华为快应用中如何实现同时传递事件对象和自定义参数
static 函数中的静态变量
HDU1234 开门人和关门人(水题)