剑指Offer_58 发表于 2017-12-06 | 分类于 剑指Offer 题目请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 解题思路解法一: 递归版本 12345678910111213141516171819202122232425262728293031323334353637/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { boolean isSymmetricalHelper(TreeNode left, TreeNode right){ if(left == null && right == null){ return true; } if((left == null && right != null) || (left != null && right == null)){ return false; } if(left.val != right.val){ return false; } return isSymmetricalHelper(left.left, right.right) && isSymmetricalHelper(left.right, right.left); } boolean isSymmetrical(TreeNode pRoot) { if(pRoot == null){ return true; } return isSymmetricalHelper(pRoot.left, pRoot.right); }} 解法二: 非递归版本