就是判断两棵树的值和结构是否相同
注意:要判断是否所有的树节点是否为NULL
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSameTree(TreeNode* p, TreeNode* q) { 13 if(!p && !q) { 14 return true; 15 } 16 else if(!p||!q){ 17 return false; 18 } 19 else{ 20 if(p->val != q->val) return false; 21 else return isSameTree(p->left, q->left) &&
isSameTree(p->right, q->right); 22 } 23 } 24 };