【LeetCode】100. 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.
分析
递归求解即可
代码
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==0&&q==0) return true;
if(p!=0&&q==0||q!=0&&p==0||p->val != q->val) return false;
return isSameTree(p->left, q->left)&&isSameTree(p->right, q->right);
}
};