剑指offer 58.对称的二叉树

 58.对称的二叉树

题目描述

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
 1 /*
 2 struct TreeNode {
 3     int val;
 4     struct TreeNode *left;
 5     struct TreeNode *right;
 6     TreeNode(int x) :
 7             val(x), left(NULL), right(NULL) {
 8     }
 9 };
10 */
11 class Solution {
12 public:
13     
14     bool isSymmetrical(TreeNode* pRoot)
15     {
16         return pRoot == NULL || judge(pRoot->left, pRoot->right);
17     }
18     
19     // 先判断当前node1 和 node2 两个结点是否相等,如果相等,判断node1的左子树是否等于node2的右子树
20     // node1的左子树是否等于node2的左子树
21     bool judge(TreeNode* node1, TreeNode* node2){
22         if(node1 == NULL && node2 == NULL){
23             return true;
24         }else if(node1 == NULL || node2 == NULL){
25             return false;
26         }
27         
28         if(node1->val != node2->val){
29             return false;
30         }else{
31             return judge(node1->left, node2->right) && judge(node1->right, node2->left);
32         }
33     }
34 
35 };
原文地址:https://www.cnblogs.com/hi3254014978/p/12332704.html