标签:
https://leetcode.com/problems/same-tree/
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 //均为空相同 14 if (p == NULL && q == NULL) 15 return true; 16 17 //其一为空不同 18 if (p == NULL || q == NULL) 19 return false; 20 21 //值不相等不同 22 if (p->val != q->val) 23 return false; 24 25 //比较左右子树是否相同 26 return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 27 } 28 };
标签:
原文地址:http://www.cnblogs.com/wanpengcoder/p/5245792.html