/** * Definition for a binary tree node. * 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==NULL) return q==NULL; if(q==NULL) return p==NULL; if(p->left==NULL && p->right ==NULL){ {if(q->left==NULL && q->right ==NULL) return q->val == p->val; }return false; } if(q->left==NULL && q->right ==NULL){ {if(p->left==NULL && p->right ==NULL) return q->val == p->val; }return false; } if(!isSameTree(p->left, q->left)) return false; if(!isSameTree(p->right, q->right)) return false; if(p->val != q->val) return false; //注意,思维的严谨性 return true; } };