/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return isSymmetric(root, root);
}
private:
bool isSymmetric(TreeNode* p, TreeNode* q) {
if (!p || !q)
return p == q;
return p->val == q->val &&
isSymmetric(p->left, q->right) &&
isSymmetric(p->right, q->left);
}
};
leetcode
challenge
Here is the link for the problem:
https://leetcode.com/problems/symmetric-tree
Top comments (0)