100. Same Tree

梦里梦外; 2022-06-10 14:10 218阅读 0赞

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.

0ms 37.5%

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
int i,j;
if(q == NULL && p == NULL )
return true;
if(p == NULL && q != NULL )
return false;
if(q == NULL && p != NULL )
return false;
if( p->val != q->val )
return false;
return isSameTree(p->right,q->right) && isSameTree(p->left,q->left);
}

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读

    相关 #100 Same Tree

    按照通过率,第三的需要买书(还是过段时间再说吧……),那么就来做第四题咯 [\100 Same Tree][100 Same Tree] 这道题同样很简单,一看就是需