!-- Loading 底层遮罩 -->

P1229 遍历问题

感谢所有AC

 

传送门

思路

    在一棵二叉树上若有一个结点是只有一个子结点,那么这个子结点在左在右不影响先序后序的遍历顺序。 先序遍历中,如果A只有一个子树B,那么在先序遍历中A一定是B的前驱,在后序遍历中A一定是B的后继。

代码

#include<iostream>
#include<cstring>
using namespace std;
string s1, s2;
int main(void)
{
    long long n = 0;
    cin >> s1 >> s2;
    for (int i = 0; i < s1.size(); i++)
    {
        int pos = s2.find(s1[i]);
        if (pos) {
            if (s2[pos - 1] == s1[i + 1])n++;
        }
    }
    cout << (1 << n);
    return 0;
}
 

 

posted @ 2022-03-30 22:01  Thinker-X  阅读(33)  评论(0)    收藏  举报