树的子结构
2016-01-06 11:26
225 查看
来自《剑指offer》的面试题18。
题目:输入两棵二叉树A和B,推断B是不是A的子结构。二叉树节点定义例如以下:
思路分析:
首先,在Tree1中找到和Tree2的根节点的值一样的结点R。
然后,再推断Tree1中以R为根结点的子树是不是包括和Tree2一样的结构。
分析演示样例:
![](http://img.blog.csdn.net/20150627101325802?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWl5aXd1ZGlhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
解决思路代码:
这里两处推断均使用了递归。详见代码。
測试代码:
測试数据为:
8 8 9 # # 2 4 # # 7 # # 7 # #
8 9 # # 2 # #
执行结果:
![](http://img.blog.csdn.net/20150627102051868?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWl5aXd1ZGlhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
牛客网測试地址:http://www.nowcoder.com/books/coding-interviews/6e196c44c7004d15b1610b9afca8bd88?rp=1
题目:输入两棵二叉树A和B,推断B是不是A的子结构。二叉树节点定义例如以下:
public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }
思路分析:
首先,在Tree1中找到和Tree2的根节点的值一样的结点R。
然后,再推断Tree1中以R为根结点的子树是不是包括和Tree2一样的结构。
分析演示样例:
解决思路代码:
这里两处推断均使用了递归。详见代码。
public class Solution { public boolean HasSubtree(TreeNode root1, TreeNode root2) { // 在Tree1中查找是否有Tree2的根节点(实际上就是树的遍历) boolean result = false; if (root1 != null && root2 != null) { // 边界条件的检查 if (root1.val == root2.val) { result = DosTree1HasTree2(root1, root2); } if (!result) { result = HasSubtree(root1.left, root2); } if (!result) { result = HasSubtree(root1.right, root2); } } return result; } public boolean DosTree1HasTree2(TreeNode root1, TreeNode root2) { // 推断Tree1中以R为根节点的子树是不是和树B具有同样的结构(即左右子树是否同样) if (root2 == null) { // 递归终止条件到达了Tree1或Tree2的叶节点。 return true; } if (root1 == null) { return false; } if (root1.val != root2.val) { return false; } return DosTree1HasTree2(root1.left, root2.left) && DosTree1HasTree2(root1.right, root2.right); // 递归推断左右子树 } }
測试代码:
// 进行測试 public class Main { Scanner scanner = new Scanner(System.in); public TreeNode createTree(TreeNode root) { String val = scanner.next(); if (val.equals("#")) { return null; } root = new TreeNode(Integer.parseInt(val)); root.left = createTree(root.left); root.right = createTree(root.right); return root; } public static void main(String[] args) { TreeNode root1 = null; TreeNode root2 = null; Main testMain = new Main(); Solution testSolution = new Solution(); boolean result = false; System.out.println("Create Tree 1:"); root1 = testMain.createTree(root1); System.out.println("Create Tree 2:"); root2 = testMain.createTree(root2); result = testSolution.HasSubtree(root1, root2); System.out.println("The result is: " + result); } }
測试数据为:
8 8 9 # # 2 4 # # 7 # # 7 # #
8 9 # # 2 # #
执行结果:
牛客网測试地址:http://www.nowcoder.com/books/coding-interviews/6e196c44c7004d15b1610b9afca8bd88?rp=1
相关文章推荐
- oracle rac cluster服务进程介绍
- 关于Node的async包的一点笔记
- volatile关键字的简单介绍
- mac添加环境变量
- ffmpeg and ffplay mac安装方法
- VS DEBUG
- 《Improving Backgroud Subtraction using local binary similarity patterns》
- 3D图形学坐标系变换
- Web容器自动对HTTP请求中参数进行URLDecode处理
- window.onerror 错误监听,发到后台
- mfc_Demo
- exp/imp使用
- Objective-C方法与函数的区别
- asp.net获取ListView与gridview中当前行的行号
- Java 实验:流
- 图片压缩
- Objective-C方法与函数的区别
- C语言运算符优先级与结合性
- AndroidStudio自定义皮肤主题-ColorThemes
- Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null objec