代码实现:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/ bool compare(struct TreeNode *tree1, struct TreeNode *tree2) {if (tree1 == NULL && tree2 != NULL) {return false;} else if (tree1 != NULL && tree2 == NULL) {return false;} else if (tree1 == NULL && tree2 == NULL) {return true;} else if (tree1->val != tree2->val) {return false;} // 此时就是:左右节点都不为空,且数值相同的情况// 此时才做递归,做下一层的判断bool compareLeft = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左bool compareRight = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右bool isSame = compareLeft && compareRight; // 左子树:中、 右子树:中(逻辑处理)return isSame; }bool isSameTree(struct TreeNode *p, struct TreeNode *q) {return compare(p, q); }