59.对称的二叉树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ling_hun_pang_zi/article/details/81664943

59.对称的二叉树

题目描述

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

struct TreeNode {
     int val;
     struct TreeNode *left;
     struct TreeNode *right;
     TreeNode(int x)
          : val(x)
          , left(NULL)
          , right(NULL)
     {}
};
class Solution {
public:
     bool isSymmetrical(TreeNode* pRoot)
     {
          if (pRoot == NULL)
              return true;
          return Symmetrical(pRoot, pRoot);
     }
     bool Symmetrical(TreeNode* pRoot1, TreeNode* pRoot2)
     {
          if (pRoot1 == NULL && pRoot2 == NULL)
              return true;
          if (pRoot1 == NULL || pRoot2 == NULL)
              return false;
          if (pRoot1->val != pRoot2->val)
              return false;
          return Symmetrical(pRoot1->left, pRoot2->right) && Symmetrical(pRoot1->right, pRoot2->left);
     }
};

猜你喜欢

转载自blog.csdn.net/ling_hun_pang_zi/article/details/81664943