Leetcode: Same tree

 

 1 class Solution {
 2 public:
 3     bool isSameTree(TreeNode *p, TreeNode *q) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (p == NULL && q == NULL)
 7             return true;
 8         if ((p == NULL && q != NULL) || (p != NULL && q == NULL))
 9             return false;
10         if (p->val == q->val)
11             return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
12         return false;
13         
14     }
15 };

 

posted @ 2013-04-30 10:53  caijinlong  阅读(116)  评论(0编辑  收藏  举报