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.



void traverse(struct TreeNode* p, struct TreeNode* q, bool *f)
{
	if (!*f)
		return;
	if (p == NULL&&q != NULL || p != NULL&&q == NULL)
	{
		*f = false;
		return;
	}
	if (p != NULL&&q != NULL)
	{
		if (p->val != q->val)
		{
			*f = false;
			return;
		}
		traverse(p->left, q->left,f);
		traverse(p->right, q->right,f);
	}
	return;
}

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
	bool f = true;
	traverse(p, q, &f);
	return f;
}

accept


你可能感兴趣的:(LeetCode)