100. Same Tree
Total Accepted: 122794 Total Submissions: 284606 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Code:
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
return (p == NULL && q == NULL) ||
((p != NULL && q != NULL && p->val == q->val) &&
(isSameTree(p->left, q->left) && isSameTree(p->right, q->right)));
}