题解 | #对称的二叉树#
对称的二叉树
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) { // write code here if (pRoot==null) return true; return judgeTwoTree(pRoot.left, pRoot.right); } public boolean judgeTwoTree(TreeNode p1,TreeNode p2){ if (p1==null) return p2==null; if (p2==null) return p1==null; if (p1.val!=p2.val) return false; return judgeTwoTree(p1.left,p2.right)&&judgeTwoTree(p1.right,p2.left); } }