Leetcode: Same Tree

阅读 70

2022-08-01


题目:

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.

解答:

bool isSameTree(TreeNode *p, TreeNode *q)
{
if (!p && !q)
{
return true;
}
if (!p && q || p && !q || p->val != q->val)
{
return false;
}
bool left = isSameTree(p->left, q->left);
bool right = isSameTree(p->right, q->right);
return left && right;
}



精彩评论(0)

0 0 举报