7-1 树的同构 (25 分)

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。

 

 

图1

图2

现给定两棵树,请你判断它们是否是同构的。

 

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:

如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):

8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -

输出样例1:

Yes

输入样例2(对应图2):

8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:

No

AC代码:

import java.util.Scanner;

class TNode {
	private char data;
	private TNode lchild_ptr, rchild_ptr;
	
	public TNode(char data, TNode lchild_ptr, TNode rchild_ptr) {
		this.data = data;
		this.lchild_ptr = lchild_ptr;
		this.rchild_ptr = rchild_ptr;
	}
	public char getData() {
		return data;
	}
	public void setData(char data) {
		this.data = data;
	}
	public TNode getLchild_ptr() {
		return lchild_ptr;
	}
	public void setLchild_ptr(TNode lchild_ptr) {
		this.lchild_ptr = lchild_ptr;
	}
	public TNode getRchild_ptr() {
		return rchild_ptr;
	}
	public void setRchild_ptr(TNode rchild_ptr) {
		this.rchild_ptr = rchild_ptr;
	}
}

class Massage {
	private char ch;
	private int lchild_no;
	private int rchild_no;
	
	public char getCh() {
		return ch;
	}
	public void setCh(char ch) {
		this.ch = ch;
	}
	public int getLchild_no() {
		return lchild_no;
	}
	public void setLchild_no(int lchild_no) {
		this.lchild_no = lchild_no;
	}
	public int getRchild_no() {
		return rchild_no;
	}
	public void setRchild_no(int rchild_no) {
		this.rchild_no = rchild_no;
	}
	
	public String toString() {
		return "ch = " + this.getCh() + " lchild = " + this.getLchild_no() + " rchild = " + this.getRchild_no();
	}
	
	public Massage(char ch, char lchild, char rchild) {
		this.ch = ch;
		
		if(lchild == '-') {
			this.lchild_no = -1;
		} else if(lchild != '-') {
			this.lchild_no = lchild - '0';
		}
		
		if(rchild == '-') {
			this.rchild_no = -1;
		} else if(rchild != '-') {
			this.rchild_no = rchild - '0';
		}
	}
}

public class Main {

	static boolean[] vis1 = null, vis2 = null;
	static Massage[] massage_tree1 = null, massage_tree2 = null;
	static int root_tree1 = -1, root_tree2 = -1;
	static TNode root1 = null, root2 = null; 
	
	public static int find_root(boolean[] vis) {
		for(int i = 0; i < vis.length; i++) {
			if(!vis[i]) {
				return i;
			}
		}
		return -1;
	}
	
	public static TNode build_tree(Massage[] massages, int pos) {
		if(pos == -1) {
			return null;
		}
		
		TNode lchild = build_tree(massages, massages[pos].getLchild_no());
		TNode rchild = build_tree(massages, massages[pos].getRchild_no());
		
		return new TNode(massages[pos].getCh(), lchild, rchild);
	}
	
	public static boolean isIsomorphism(TNode tree1, TNode tree2) {
		if(tree1 == null && tree2 == null) {
			return true;
		}
		if(tree1 == null && tree2 != null) {
			return false;
		}
		if(tree1 != null && tree2 == null) {
			return false;
		}
		
		if(tree1.getData() != tree2.getData()) {
			return false;
		}
		
		if(isIsomorphism(tree1.getLchild_ptr(), tree2.getLchild_ptr()) && isIsomorphism(tree1.getRchild_ptr(), tree2.getRchild_ptr())) {
			return true;
		}
		
		if(isIsomorphism(tree1.getLchild_ptr(), tree2.getRchild_ptr()) && isIsomorphism(tree1.getRchild_ptr(), tree2.getLchild_ptr())) {
			return true;
		}
		
		return false;
	}
	
	
	public static Massage[] read_tree(Scanner scanner) {
		int n = scanner.nextInt();
		scanner.nextLine();
		Massage[] massages = new Massage[n];
		
		for(int i = 0; i < n; i++) {
			String string = scanner.nextLine();
			massages[i] = new Massage(string.charAt(0), string.charAt(2), string.charAt(4));
		}
		
		return massages;
	}
	
	public static boolean[] get_rootno(Massage[] massages) {
		boolean[] vis = new boolean[massages.length];
		
		for(int i = 0; i < massages.length; i++) {
			if(massages[i].getLchild_no() != -1) {
				vis[massages[i].getLchild_no()] = true;
			}
			if(massages[i].getRchild_no() != -1) {
				vis[massages[i].getRchild_no()] = true;
			}
		}
		
		return vis;
	}
	
	public static void main(String []args) {
		Scanner scanner = new Scanner(System.in);
		
		massage_tree1 = read_tree(scanner);
		massage_tree2 = read_tree(scanner);
		
		vis1 = get_rootno(massage_tree1);
		vis2 = get_rootno(massage_tree2);
		
		if(massage_tree1.length != massage_tree2.length) {
			System.out.println("No");
		} else if(massage_tree1.length == massage_tree2.length) {
			root_tree1 = find_root(vis1);
			root_tree2 = find_root(vis2);
			
			root1 = build_tree(massage_tree1, root_tree1);
			root2 = build_tree(massage_tree2, root_tree2);
			
			if(isIsomorphism(root1, root2)) {
				System.out.println("Yes");
			} else {
				System.out.println("No");
			}
		}
		
		scanner.close();
	}
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值