Same Tree [LeetCode]

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

 1 class Solution {
 2 public:
 3     bool isSameTree(TreeNode *p, TreeNode *q) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         if(p == NULL && q == NULL)
 6             return true;
 7         if(p == NULL && q != NULL ||
 8             (p != NULL && q == NULL))
 9             return false;
10         if(p->val != q->val)
11             return false;
12         return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 
13     }
14 };

 

posted @ 2013-10-18 06:14  假日笛声  阅读(151)  评论(0编辑  收藏  举报