这个题相对来说是比较简单的,思路为:利用先序遍历来每个点进行判断
struct TreeNode {
int value;
TreeNode *leftTreeNode;
TreeNode *rightTreeNode;
};
bool isSameTree(TreeNode *firstTreeNode, TreeNode *secondTreeNode) {
if (firstTreeNode == NULL && secondTreeNode == NULL) { // 两个点都为空
return true;
} else if (firstTreeNode == NULL || secondTreeNode == NULL) { // 其中一个点为空
return false;
} else { // 两个点都不为空
if (firstTreeNode->value == secondTreeNode->value) {
return isSameTree(firstTreeNode->leftTreeNode, secondTreeNode->leftTreeNode) && isSameTree(firstTreeNode->rightTreeNode, secondTreeNode->rightTreeNode);
} else {
return false;
}
}
}