Skip to content

Instantly share code, notes, and snippets.

@beyondkmp
Created July 22, 2015 06:29
Show Gist options
  • Select an option

  • Save beyondkmp/119d5a2c745896684a3f to your computer and use it in GitHub Desktop.

Select an option

Save beyondkmp/119d5a2c745896684a3f to your computer and use it in GitHub Desktop.
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isChildSymmetric(struct TreeNode* p, struct TreeNode* q) {
if(!p && !q)
return true;
if(!p || !q)
return false;
return((p->val==q->val) && isChildSymmetric(p->left,q->right) && isChildSymmetric(p->right,q->left));
}
bool isSymmetric(struct TreeNode* root) {
if(!root)
return true;
return isChildSymmetric(root->left,root->right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment