0
点赞
收藏
分享

微信扫一扫

Leetcode: Same Tree

水墨_青花 2022-08-01 阅读 70


题目:

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 条评论