1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

bool isSameTree(TreeNode *p, TreeNode *q) {
    if (p == NULL && q == NULL) return true;
    
    if (p != NULL && q != NULL) {
        if (p->val != q->val) {
            return false;
        }

        if (!isSameTree(p->left, q->left)) {
            return false;
        }

        return isSameTree(p->right, q->right);
    }

    //one node is NULL, the other is not NULL
    return false;
}
View Program Text


Test Status