56、对称的二叉树

一、题目

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

二、解法

 1 /*
 2 public class TreeNode {
 3     int val = 0;
 4     TreeNode left = null;
 5     TreeNode right = null;
 6 
 7     public TreeNode(int val) {
 8         this.val = val;
 9 
10     }
11 
12 }
13 */
14 public class Solution {
15     boolean isSymmetrical(TreeNode pRoot)
16     {
17         if(pRoot == null)
18             return true;
19         return comRoot(pRoot.left,pRoot.right);
20     }
21     private boolean comRoot(TreeNode left,TreeNode right){
22         if(left == null)
23             return right == null;
24         if(right == null)
25             return false;
26         if(left.val != right.val)
27             return false;
28         return comRoot(left.right,right.left)&&comRoot(left.left,right.right);
29     }
30 }
原文地址:https://www.cnblogs.com/fankongkong/p/7462099.html