• 1020.Tree Traversals [后序中序转层序(不建树/建树)]*附带[前序中序转后序(建树)||后序中序转前序(不建树)]


    题目AC代码
    思路没什么好说的,重点理解那两行遍历的递归就是了,卡在那很久,注意一个左子树的个数减去右边的子树个数这类的,附参考柳神的
    ~https://www.liuchuo.net/archives/2090
    理解了这个这一类就ok了,重点理解那个递归的过程!!!!!!!!
    #include<iostream>
    #include<vector>
    #include<map>
    using namespace std;
    vector<int>post, in;
    map<int, int>p;
    void pre(int root, int start, int end, int index) {
    	if (start > end) return;
    	int i = start;
    	while (i < end && in[i] != post[root]) i++;
    	p[index] = post[root];
    	pre(root-end+i-1, start, i - 1, 2 * index + 1);
    	pre(root - 1, i + 1, end, 2 * index + 2);
    }
    int main()
    {
    	int n; cin >> n;
    	int i;
    	for (int i = 0; i < n; i++)
    	{
    		int tmp; cin >> tmp;
    		post.push_back(tmp);
    	}
    	for (int i = 0; i < n; i++)
    	{
    		int tmp; cin >> tmp;
    		in.push_back(tmp);
    	}
    	pre(n-1, 0, n-1,0);
    	auto it = p.begin();
    	cout << it->second;
    	while (++it != p.end())
    		cout << " " << it->second;//ends并不是空格而是‘/0’
    	return 0;
    
    }
    

    后序中序转层序建树法
    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef struct node {
    	int data;
    	node* left, *right;
    }*Tree;
    vector<int>in, post;
    Tree pre(int root,int start, int end)
    {
    	if (start > end) return NULL;
    	int i = start;
    	Tree T = new node;
    	T->data = post[root];
    	while (i < end && in[i] != post[root]) i++;
    	T->left=pre(root - end + i - 1, start, i - 1);
    	T->right=pre(root - 1, i + 1, end);
    	return T;
    }
    void preprint(Tree T)
    {
    	if (T != NULL) {
    		cout << T->data;
    		preprint(T->left);
    		preprint(T->right);
    	}
    }
    void levelorder(Tree T)
    {
    	queue<Tree>q;
    	int i;
    	q.push(T);
    	int num = 0;
    	while (!q.empty()) {
    		Tree tmp = q.front();
    		q.pop();
    		if (!num)
    			cout << tmp->data;
    		else
    			cout << " " << tmp->data;
    		num++;
    		if (tmp->left)
    			q.push(tmp->left);
    		if (tmp->right)
    			q.push(tmp->right);
    	}
    }
    int main()
    {
    	Tree T;
    	int n; cin >> n;
    	in.resize(n); post.resize(n);
    	for (int i = 0; i < n; i++)
    		cin >> post[i];
    	for (int i = 0; i < n; i++)
    		cin >> in[i];
    	T = pre(n - 1, 0, n - 1);
    	//preprint(T);
    	levelorder(T);
    
    }
    

    后序中序转前序
    #include<iostream>
    #include<vector>
    using namespace std;
    vector<int>post, in;
    void pre(int root, int start, int end)
    {
    	if (start > end)return;
    	int i = start;
    	while (i < end && in[i] != post[root])
    		i++;
    	cout << post[root] <<ends;
    	pre(root - 1 - end + i, start, i - 1);
    	pre(root - 1, i + 1, end);
    }
    int main()
    {
    	int n; cin >> n;
    	int i;
    	for (int i = 0; i < n; i++)
    	{
    		int tmp; cin >> tmp;
    		post.push_back(tmp);
    	}
    	for (int i = 0; i < n; i++)
    	{
    		int tmp; cin >> tmp;
    		in.push_back(tmp);
    	}
    	pre(5, 0, 5);
    	
    }
    

    前序中序转后序
    #include<iostream>
    #include<vector>
    using namespace std;
    vector<int>pre, in;
    struct node
    {
    	int data;
    	node* left, * right;
    };
    typedef node* tree;
    tree post(int root,int start,int end)
    {
    	if (start > end)return NULL;
    	int i = start;
    	tree T= new node;
    	T->data = pre[root];
    	while (i != end && in[i] != pre[root])
    		i++;
    	T->left=post(root + 1, start, i - 1);
    	T->right=post(root + i - start + 1, i + 1, end);
    	return T;
    }
    void print(tree T)
    {
    	if (T != NULL) {
    		print(T->left);
    		print(T->right);
    		cout << " "<<T->data;
    	}
    }
    int main()
    {
    	int i,n;
    	scanf_s("%d", &n);
    	pre.resize(n);
    	in.resize(n);
    	for (i = 0; i < n; i++) scanf_s("%d", &pre[i]);
    	for (i = 0; i < n; i++) scanf_s("%d", &in[i]);
    	tree T = post(0, 0, n - 1);
    	print(T);
    	return 0;
    }
    
  • 相关阅读:
    Flink 状态生命周期
    jpa使用@CollectionTable创建表
    Java的四种引用---强软弱虚
    ThreadLocal与内存泄露
    Flink 1.11 Table API 实现kafka到mysql
    FLIink 1.11 SQL 构建一个端到端的流式应用
    Flink1.11编译
    Flink运行yarn-session报错 java.lang.NoClassDefFoundError: org/apache/hadoop/yarn/exceptions/YarnException
    欢迎订阅AI科技导读微信公众号,获取人工智能的最新技术解读教程!
    深度学习深刻理解和应用--人工智能从业人员必看知识
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109983.html
Copyright © 2020-2023  润新知