/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool compare(TreeNode* l , TreeNode* r){ if(l == NULL && r == NULL) return true; if(l == NULL && r != NULL) return false; if(l != NULL && r == NULL) return false; if(l -> val != r -> val) return false; return compare(l -> right , r -> left) && compare(l -> left , r -> right); } bool isSymmetric(TreeNode *root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(root == NULL) return true; if(root -> left == NULL && root -> right == NULL) return true; return compare(root -> left , root -> right); } };