Created
July 22, 2015 06:29
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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