/*方法1:两重递归遍历*/
bool isSameTree(struct TreeNode* s, struct TreeNode* t);
bool JudgeTree(struct TreeNode* s, struct TreeNode* t);
bool ret_val = false;
bool isSameTree(struct TreeNode* s, struct TreeNode* t)
{
if( (s == NULL)
&& (t == NULL)
)
{
return true;
}
if( (s != NULL)
&& (t != NULL)
)
{
if (s->val == t->val)
{
return isSameTree(s->left, t->left) && isSameTree(s->right, t->right); /*左侧优先,短路逻辑优化*/
}
else
{
return false;
}
}
else
{
return false;
}
}
bool JudgeTree(struct TreeNode* s, struct TreeNode* t)
{
if( (s==NULL)
&&(t==NULL)
)
{
ret_val = true;
return true;
}
if( (s==NULL)
&&(t!=NULL)
)
{
return false;
}
if( (s!=NULL)
&&(t==NULL)
)
{
return false;
}
if( (isSameTree (s , t))/*根优先,短路逻辑优化*/
||(JudgeTree (s->left , t))
||(JudgeTree (s->right, t))
)
{
ret_val = true;
return true;
}
else
{
return false;
}
}
bool isSubtree(struct TreeNode* s, struct TreeNode* t)
{
ret_val = false;
JudgeTree(s, t);
return ret_val;
}
/*
方法1:
执行用时 :32 ms, 在所有 C 提交中击败了87.30%的用户
内存消耗 :12.5 MB, 在所有 C 提交中击败了100.00%的用户
*/
|