代码没什么好解释的,一看就懂的代码~

递归

import java.util.*;

public class Solution {

    public boolean isSymmetric (TreeNode root) {
        if(root == null)
            return true;
        return check(root.left,root.right);
    }

    public boolean check(TreeNode left, TreeNode right){
        if(left == null && right == null)
            return true;
        if(left == null || right == null)
            return false;
        return left.val == right.val && check(left.left,right.right) && check(left.right,right.left);
    }
}

迭代

import java.util.*;
public class Solution {

    public boolean isSymmetric (TreeNode root) {

        if(root == null)
            return true;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root.left);
        q.offer(root.right);

        while(!q.isEmpty()){
            TreeNode left = q.poll();
            TreeNode right = q.poll();
            if(left == null && right == null)
                continue;
            if(left == null || right == null)
                return false;
            if(left.val != right.val)
                return false;

            q.offer(left.left);
            q.offer(right.right);
            q.offer(left.right);
            q.offer(right.left);
        }
        return true;
    }


}