100. Same Tree

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)));   
}

posted on 2016-04-12 10:37  Alex0111  阅读(163)  评论(0)    收藏  举报

导航