Created
April 27, 2015 12:37
-
-
Save hongruiqi/5527363c7f9d018af414 to your computer and use it in GitHub Desktop.
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
/** | |
* 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