Identical Binary Tree
题目:
Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value.
分析:
这题目从程序的逻辑上讲不复杂,但是比较考验细致的事情,第二次做的时候我忘记了判断(a == NULL || b == NULL)的情况。
解法:
class Solution {
public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
bool isIdentical(TreeNode* a, TreeNode* b) {
// Write your code here
if (a == NULL && b == NULL) {
return true;
}
if (a == NULL || b == NULL) {
return false;
}
if (a->val != b->val) {
return false;
}
return (isIdentical(a->left, b->left) && isIdentical(a->right, b->right) );
}
};