Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) {
return true;
}
return dfs(root->left, root->right);
}
bool dfs(TreeNode* l, TreeNode* r) {
if (!l && !r) {
return true;
}
if (!l || !r) {
return false;
}
if (l->val != r->val) {
return false;
}
return dfs(l->left, r->right) && dfs(l->right, r->left);
}
};