当前位置:网站首页>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
BinTree
How 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
PreOrder
ina
The successor isb
, stayPostOrder
ina
The 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;
}
边栏推荐
- 二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
- From Read and save in bag file Jpg pictures and PCD point cloud
- 2022-06-17
- Filtering of PCL
- 使用华为性能管理服务,按需配置采样率
- 快应用中实现自定义抽屉组件
- 618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
- 面对不确定性,供应链的作用
- Open the encrypted SQLite method with sqlcipher
- How to get the password of cpolar?
猜你喜欢
如何用list组件实现tabbar标题栏
Disassembling Meitu SaaS: driving the plane to change the engine
Thanos Receiver
Session cookies and tokens
2.hacking-lab脚本关[详细writeup]
【AI应用】海康威视iVMS-4200软件安装
nodejs+express+mysql简单博客搭建
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
OpenMLDB Meetup No.4 会议纪要
从MediaRecord录像中读取H264参数
随机推荐
PCL point cloud to depth image
Analysis of hot spots in AI technology industry
Kustomize user manual
首份中国企业敏捷实践白皮书发布| 附完整下载
From Read and save in bag file Jpg pictures and PCD point cloud
【AGC】构建服务3-认证服务示例
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
华为快应用中如何实现同时传递事件对象和自定义参数
【AI应用】海康威视iVMS-4200软件安装
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
面对不确定性,供应链的作用
从MediaRecord录像中读取H264参数
最详细MySql安装教程
UVM factory mechanism
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
MYSQL关键字
Nodejs+express+mysql simple blog building
计算序列之和
Oracle 笔记