[leetcode_100]Same Tree

比较两棵树是否完全一样。
一次AC,枚举即可。

 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
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
    bool ans;
    bool isSameTree(TreeNode *p, TreeNode *q) 
    {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        ans = true;
        check(p,q); 
        return ans;
    }
    void check(TreeNode *p,TreeNode *q)
    {
        if((p == NULL && q != NULL) || (p != NULL && q == NULL))
        {
            ans = false;
            return;
        }
        else
            if(p == NULL && q == NULL)
            {
                return ;
            }
            else
            {
                if(p->val != q->val)
                {
                    ans = false;
                    return;
                }
                else
                {
                    check(p->left,q->left);
                    check(p->right,q->right);
                }
            }
    }
};
Licensed under CC BY-NC-SA 4.0