Skip to content

Instantly share code, notes, and snippets.

@hongruiqi
Created April 27, 2015 12:37
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 hongruiqi/5527363c7f9d018af414 to your computer and use it in GitHub Desktop.
Save hongruiqi/5527363c7f9d018af414 to your computer and use it in GitHub Desktop.
/**
* 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 isSameTree(TreeNode *p, TreeNode *q) {
if (!p&&!q) return true;
if (!p) return false;
if (!q) return false;
if (p->val!=q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <vector>
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (!p&&!q) return true;
if (!p) return false;
if (!q) return false;
if (p->val!=q->val) return false;
return isSameTree(p->left, q->right) && isSameTree(p->right, q->left);
}
bool isSymmetric(TreeNode *root) {
if (!root) return true;
return isSameTree(root->left, root->right);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment