题解 | #对称的二叉树# | C++
对称的二叉树
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 { bool dfs(TreeNode* one, TreeNode* two) { if (one == nullptr && two == nullptr) return true; if (one == nullptr || two == nullptr) return false; return one->val == two->val && dfs(one->left, two->right) && dfs(one->right, two->left); } public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ bool isSymmetrical(TreeNode* pRoot) { return dfs(pRoot, pRoot); } };