剑指offer 对称的二叉树

xiaoxiao2021-03-01  19

题目描述

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

代码:

class Solution { public: bool isSymmetrical(TreeNode* pRoot) { if(pRoot==NULL)return true; return judge(pRoot->left,pRoot->right); } bool judge(TreeNode *root1,TreeNode *root2){ if(!root1&&!root2)return true; if(root1&&root2) return root1->val==root2->val&&judge(root1->left,root2->right)&&judge(root1->right,root2->left); return false; } };
转载请注明原文地址: https://www.6miu.com/read-3200111.html

最新回复(0)