1102. Invert a Binary Tree (25)<反转二叉树>

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it's your turn to prove that YOU CAN invert a binary tree!

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

给一个二叉树,左右结点互换,输出层序遍历和先序遍历


这种类型的题目我比较喜欢用链表做,做的最近这几题,发现直接结构体数组更容易

1099. Build A Binary Search Tree (30) <BST树>

像上面这道题一样,这类题用链表还要开辟空间。一不小心就会出现段错误

建议还是使用数组做

两种方法我也都做了


结构体数组:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef struct tree{
	//int num;
	int left,right;
}tree,linktree;
linktree t[100];
int father[100];
int n;
int cengxu[100];
int xianxu[100];
int cnt;
int find(int x){
	if(x==father[x]) return x;
	return father[x]=find(father[x]);
}
void pro(int x){
	if(x!=-1){
		pro(t[x].left);
		xianxu[cnt++]=x;
		//cout<<t[x].num<<" ";
		pro(t[x].right);
	}
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++) father[i]=i;
    for(int i=0;i<n;i++){
    	char a,b;
    	cin>>a>>b;
    	if(a=='-'){
    		t[i].right=-1;
		}else{
			t[i].right=a-'0';
			father[a-'0']=i;
		}
		if(b=='-'){
    		t[i].left=-1;
		}else{
			t[i].left=b-'0';
			father[b-'0']=i;
		}
	}
	int start=find(0);
	queue<int> que;
	que.push(start);
	cnt=0;
	while(que.size()){
		int num=que.front();
		que.pop();
		cengxu[cnt++]=num;
		if(t[num].left!=-1) que.push(t[num].left); 
		if(t[num].right!=-1) que.push(t[num].right); 
	}
	for(int i=0;i<n;i++) {
       if(i==0) cout<<cengxu[i];
       else cout<<" "<<cengxu[i];
    }
	cnt=0;
	pro(start);
	
    cout<<endl;
    for(int i=0;i<n;i++) {
       if(i==0) cout<<xianxu[i];
       else cout<<" "<<xianxu[i];
    }
	return 0;
}


链表:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef struct tree{
	int num;
	struct tree *left,*right;
}tree,*linktree;
linktree t[100];
int father[100];
int find(int x){
	if(x==father[x]) return x;
	return father[x]=find(father[x]);
}
int n;
int cengxu[100];
int xianxu[100],c=0;
void pro(linktree head){
	if(head){
		pro(head->left);
		xianxu[c++]=head->num;
		pro(head->right);
	}
}
int main(){
    int v[100]={0};
    cin>>n;
    for(int i=0;i<n;i++) father[i]=i;
    for(int i=0;i<n;i++){
    	char a,b;
    	cin>>a>>b;
    	
    	if(v[i]==0){
    		t[i]=new tree();
    		v[i]=1;
		}
		t[i]->num=i;
    	if(a=='-'){
    		t[i]->right=NULL;
		}
		else{
			if(v[a-'0']==0){
				v[a-'0']=1;
				t[a-'0']=new tree();
			}
			t[a-'0']->num=a-'0';
			t[i]->right=t[a-'0'];
			father[a-'0']=i;
		}
		if(b=='-'){
			t[i]->left=NULL;
		}else{
			if(v[b-'0']==0){
				v[b-'0']=1;
				t[b-'0']=new tree();
			}
			t[b-'0']->num=b-'0';
			t[i]->left=t[b-'0'];
			father[b-'0']=i;
		}
	} 
	int start=find(0);
//	cout<<start<<endl; 
	queue<linktree> que;
	que.push(t[start]);
	int cnt=0;
	while(que.size()){
	   linktree fz=new tree();
	   fz=que.front();
	   que.pop();
	   cengxu[cnt++]=fz->num;
	   if(fz->left) que.push(fz->left);
	   if(fz->right) que.push(fz->right);
	} 
	pro(t[start]);
	for(int i=0;i<n;i++) {
		if(i==0) cout<<cengxu[i];
		else cout<<" "<<cengxu[i];
	}
	cout<<endl;
	for(int i=0;i<n;i++) {
		if(i==0) cout<<xianxu[i];
		else cout<<" "<<xianxu[i];
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值