C++/代码:
class Solution { public: bool isSymmetrical(TreeNode* pRoot) { if (!pRoot) return true; return dfs(pRoot->left,pRoot->right); } bool dfs(TreeNode* p1, TreeNode* p2) { if (!p1 || !p2) return !p1 && !p2; //一个为空时,返回false,两个为空时,返回true if (p1->val != p2->val) return false; //判定值是否相同 return dfs(p1->left,p2->right) && dfs(p2->left,p1->right); } };