C++
1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 public: 15 /** 16 * @aaram a, b, the root of binary trees. 17 * @return true if they are identical, or false. 18 */ 19 bool isIdentical(TreeNode* a, TreeNode* b) { 20 // Write your code here 21 if (a == NULL && b == NULL) { 22 return true; 23 } 24 if (a == NULL || b == NULL) { 25 return false; 26 } 27 if (a->val != b->val) { 28 return false; 29 } 30 return isIdentical(a->left, b->left) && isIdentical(a->right, b->right); 31 } 32 };