给定彼此独立的两棵二叉树,判断 t1 树是否有与 t2 树拓扑结构完全相同的子树。
设 t1 树的边集为 E1,t2 树的边集为 E2,若 E2 等于 E1 ,则表示 t1 树和t2 树的拓扑结构完全相同。
第一行输入两个整数 n 和 root,n 表示二叉树 t1 的总节点个数,root 表示二叉树 t1 的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
第 n+2 行输入两个整数 m 和 root,n 表示二叉树 t2 的总节点个数,root 表示二叉树 t2 的根节点。
以下 m 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
如果 t1 树有与 t2 树拓扑结构完全相同的子树,则输出 "true",否则输出 "false"。
9 1 1 2 3 2 4 5 4 0 8 8 0 0 5 9 0 9 0 0 3 6 7 6 0 0 7 0 0 5 2 2 4 5 4 0 8 8 0 0 5 9 0 9 0 0
true
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.HashMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 构建树A String[] params = br.readLine().split(" "); int n = Integer.parseInt(params[0]); TreeNode treeA = new TreeNode(Integer.parseInt(params[1])); HashMap<Integer, TreeNode> map = new HashMap<>(); map.put(treeA.val, treeA); for(int i = 0; i < n; i++){ params = br.readLine().split(" "); int val = Integer.parseInt(params[0]); int leftVal = Integer.parseInt(params[1]); int rightVal = Integer.parseInt(params[2]); TreeNode node = map.get(val); if(leftVal != 0) { node.left = new TreeNode(leftVal); map.put(leftVal, node.left); } if(rightVal != 0) { node.right = new TreeNode(rightVal); map.put(rightVal, node.right); } } // 构建树B params = br.readLine().split(" "); int m = Integer.parseInt(params[0]); TreeNode treeB = new TreeNode(Integer.parseInt(params[1])); map.clear(); map.put(treeB.val, treeB); for(int i = 0; i < m; i++){ params = br.readLine().split(" "); int val = Integer.parseInt(params[0]); int leftVal = Integer.parseInt(params[1]); int rightVal = Integer.parseInt(params[2]); TreeNode node = map.get(val); if(leftVal != 0) { node.left = new TreeNode(leftVal); map.put(leftVal, node.left); } if(rightVal != 0) { node.right = new TreeNode(rightVal); map.put(rightVal, node.right); } } System.out.println(isSubStructure(treeA, treeB)); } private static boolean isSubStructure(TreeNode treeA, TreeNode treeB) { if(treeA == null || treeB == null) return false; // 空树不是任何一棵树的子树 // 树A和树B完全一样,或树B为树A的左右子树之一都可以 return isSame(treeA, treeB) || isSubStructure(treeA.left, treeB) || isSubStructure(treeA.right, treeB); } private static boolean isSame(TreeNode treeA, TreeNode treeB) { if(treeB == null) return true; // B树到空,说明节点都检查完了,返回true if(treeA == null || treeA.val != treeB.val) return false; // A树为空或节点值不同,返回false // 检查两棵树的左右子树是否都相同 return isSame(treeA.left, treeB.left) && isSame(treeA.right, treeB.right); } } class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val) { this.val = val; } }