剑指offer计划7（搜索与回溯算法简单版）---java

1.2、解法

(A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));

1.3、代码

``````class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
}
boolean recur(TreeNode A, TreeNode B) {
if(B == null) return true;
if(A == null || A.val != B.val) return false;
return recur(A.left, B.left) && recur(A.right, B.right);
}
}

``````

2.3、代码

``````class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root==null) return null;
TreeNode left = root.left;
root.left=root.right;
root.right=left;
root.left=mirrorTree(root.left);
root.right=mirrorTree(root.right);
return root;
}
}

``````

3.3、代码

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return root==null?true:recur(root.left,root.right);
}
boolean recur (TreeNode a,TreeNode b){
if(a==null && b==null) return true;
if(a==null || b==null ||a.val!=b.val)  return false;
return recur(a.left,b.right) && recur(a.right,b.left);
}
}

``````

(0)
(0)