题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ bool traversal(TreeNode* left, TreeNode* right) { if (right == NULL && left == NULL) return true; if (right != NULL && left == NULL) return false; if (right == NULL && left != NULL) return false; if (left->val != right->val) return false; return traversal(left->left, right->right) && traversal(left->right, right->left); } bool isSymmetrical(TreeNode* pRoot) { // write code here if (!pRoot) return true; return traversal(pRoot->left, pRoot->right); } };