Last active
August 29, 2015 14:04
-
-
Save xiren-wang/00705240b62959689d97 to your computer and use it in GitHub Desktop.
same binary tree
This file contains 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
/* | |
struct TreeNode { | |
int value; | |
TreeNode *left; | |
TreeNode *right; | |
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
}; | |
*/ | |
bool isSameTree(TreeNode *p, TreeNode *q) { | |
//both empty, OK | |
if (!p && !q) | |
return true; | |
//if only single empty, No | |
if (!p ) | |
return false; | |
if (!)q | |
return false; | |
//now both are not empty | |
if (p->value != q->value) | |
return false; | |
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment