[LeetCode] Same Tree

http://oj.leetcode.com/problems/same-tree/

 1 class Solution {
 2 public:
 3     bool isSameTree(TreeNode *p, TreeNode *q) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         if (p == NULL && q == NULL) {
 7             return true;
 8         } else if ((p == NULL && q != NULL) || (p != NULL && q == NULL)) {
 9             return false;
10         }
11         if (p->val == q->val) {
12             return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
13         } else {
14             return false;
15         }
16     }
17 };

 

posted @ 2013-11-06 01:21  NextLife  阅读(162)  评论(0)    收藏  举报