Leetcode ☞ 100. Same Tree

100. Same Tree

My Submissions
Question
Total Accepted: 116251  Total Submissions: 270780  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.








我的AC:

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    if(!p && !q) return true;
    if (!p || !q ) return false;
    
    bool ans = (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    return ans;
}

























你可能感兴趣的:(Leetcode ☞ 100. Same Tree)