Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiren-wang/00705240b62959689d97 to your computer and use it in GitHub Desktop.
Save xiren-wang/00705240b62959689d97 to your computer and use it in GitHub Desktop.
same binary tree
/*
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