[LeetCode] Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
做过symmetric tree再做这题就简单了,同时深搜两颗树,同时对比他们的左孩子和右孩子就可以了。
bool isSameTree(TreeNode *p, TreeNode *q) { if (!p && !q) return true; if (!(p && q)) return false; if (p->val == q->val) { return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } return false; }