标签:
1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solution { 15 public boolean HasSubtree(TreeNode root1,TreeNode root2) { 16 boolean res = false ; 17 if(root1!=null&&root2!=null){ 18 if(root1.val==root2.val){ 19 res = doesHas(root1,root2); 20 } 21 if(!res){ 22 res = HasSubtree(root1.left,root2) ; 23 } 24 if(!res){ 25 res = HasSubtree(root1.right,root2) ; 26 } 27 } 28 return res ; 29 } 30 private boolean doesHas(TreeNode root1, TreeNode root2) { 31 if(root2==null){ 32 return true ; 33 } 34 if(root1==null){ 35 return false ; 36 } 37 if(root1.val!=root2.val){ 38 return false ; 39 } 40 41 return doesHas(root1.left,root2.left)&&doesHas(root1.right,root2.right); 42 } 43 }
标签:
原文地址:http://www.cnblogs.com/huntertoung/p/4777251.html