标签:false style val nbsp OLE return div 剑指offer code
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 if(root1==null || root2==null){ 17 return false; 18 } 19 boolean result=false; 20 if(root1!=null && root2!=null){ 21 if(root1.val==root2.val){ 22 result=doesTree1HaveTree2(root1,root2); 23 } 24 if(!result){ 25 result=HasSubtree(root1.left,root2); 26 } 27 if(!result){ 28 result=HasSubtree(root1.right,root2); 29 } 30 31 } 32 return result; 33 } 34 public boolean doesTree1HaveTree2(TreeNode root1,TreeNode root2){ 35 if(root2==null){ 36 return true; 37 } 38 if(root1==null){ 39 return false; 40 } 41 if(root1.val!=root2.val){ 42 return false; 43 } 44 return doesTree1HaveTree2(root1.left,root2.left) && doesTree1HaveTree2(root1.right,root2.right); 45 } 46 }
标签:false style val nbsp OLE return div 剑指offer code
原文地址:https://www.cnblogs.com/wangqiong/p/11762052.html