1127 ZigZagging on a Tree (30 point(s))

1127 ZigZagging on a Tree (30 point(s))

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

zigzag.jpg

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

Sample Output:

1 11 5 8 17 12 20 15

重建二叉树、层次遍历、STL的使用。

重建二叉树注意点:

(1)循环跳出条件(即如果左子树/右子树不存在,left>right的时候返回nullptr);

(2)需要返回根节点。

后续内容我的做法:

采用BFS层次遍历过程中,给孩子结点更新所在层次,用vector<Node*> 存储结点该当前的结点,当读取到下一层次的结点前,先输出该层次的结点的数据(根据情况决定是否反转)。做法过于复杂。

大神思路:

(1)静态存储结点。

(2)DFS遍历,将同一层次的结点分别存储(是从左到右),再根据所在层次决定是否反转。BFS也可以。

 大神链接:BFS/DFS

https://blog.csdn.net/richenyunqi/article/details/80171623

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAX = 37;
int post[MAX];int in[MAX];
struct Node{
	int data;Node* left;Node* right;
	int level;
	Node(int d):data(d),left(nullptr),right(nullptr){}
};
int N;
Node* createTree(int root,int left,int right){
	//根据后序遍历的root位置,从中序的left和right区间确定左子树和右子树
	if(left>right) return nullptr;
	Node* cur = new Node(post[root]);
	int idx = left;
	while(post[root]!=in[idx])
		idx++;
	cur->left = createTree(root-right+idx-1,left,idx-1);
	cur->right = createTree(root-1,idx+1,right);
	return cur; 
}
void output(vector<int> &v,int curLevel){
	for(auto i:v){
		if(curLevel==0) cout<<i;
		else cout<<" "<<i;
	}
	v.clear();
}
void levelOrder(Node* &root){
	queue<Node*> q;
	vector<int> v;
	q.push(root);
	int curLevel = 0;
	while(!q.empty()){
		Node* top = q.front();
		q.pop();
		if(top->level>curLevel){
			if(curLevel%2==0) reverse(v.begin(),v.end());
			output(v,curLevel);
			curLevel = top->level;
		}
		v.push_back(top->data);
		if(top->left!=nullptr){
			top->left->level = top->level+1;
			q.push(top->left);
		}
		if(top->right!=nullptr){
			top->right->level = top->level+1;
			q.push(top->right);
		}
	}
	if(!v.empty()){
		if(curLevel%2==0) reverse(v.begin(),v.end());
		output(v,curLevel);
	}
}
int main(void){
	cin>>N;
	for(int i=0;i<N;i++) cin>>in[i];
	for(int i=0;i<N;i++) cin>>post[i];
	Node* root = createTree(N-1,0,N-1);
	root->level =0;
	levelOrder(root);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值