一、题目内容

image.png

二、题解

解法1:

思路

代码

  1. public class Solution {
  2. public boolean isContains (TreeNode root1, TreeNode root2) {
  3. // write code here
  4. if(root1 == null){
  5. return false;
  6. }
  7. if(root1.val!=root2.val){
  8. return isContains(root1.left,root2) || isContains(root1.right,root2);
  9. }else{
  10. return isSame(root1,root2);
  11. }
  12. }
  13. private boolean isSame(TreeNode root1, TreeNode root2){
  14. if(root1 == null&&root2==null){
  15. return true;
  16. }
  17. if(root1==null||root2==null){
  18. return false;
  19. }
  20. return (root1.val == root2.val) && isSame(root1.left,root2.left) && isSame(root1.right,root2.right);
  21. }
  22. }