题解 | #对称的二叉树#
对称的二叉树
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 return resuYesno(pRoot, pRoot); } boolean resuYesno(TreeNode pRoot1,TreeNode pRoot2){ if(pRoot1 == null && pRoot2 == null){ return true; } if(pRoot1 == null || pRoot2 == null || pRoot1.val != pRoot2.val) { return false; } return resuYesno(pRoot1.left,pRoot2.right) && resuYesno(pRoot1.right,pRoot2.left); } }