与 ”相同二叉树“ 类似
一、深度优先
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode p,TreeNode q){
if(p==null&&q==null) return true;
if(p==null||q==null||q.val!=p.val) return false;
return isSymmetric(p.left,q.right)&&isSymmetric(p.right,q.left);
}
}
二、广度
略