题解 | #对称的二叉树# 递归
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ public boolean isSymmetrical (TreeNode pRoot) { if (pRoot == null) { return true; } return isSymmetrical(pRoot.left, pRoot.right); } public boolean isSymmetrical(TreeNode left, TreeNode right) { if (left == null && right == null) { return true; } if (left == null || right == null) { return false; } if (left.val != right.val) { return false; } return isSymmetrical(left.left, right.right) && isSymmetrical(left.right, right.left); } }