aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/leetcode/cpp/965.cpp
blob: fd2bff60027cc30a7e2f11d7c0b71f94950b46a7 (plain)
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
#include <cstddef>

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

class Solution
{
public:
    static bool traverse(TreeNode *root, int val)
    {
        if (root == NULL)
            return true;
        if (root->val != val)
            return false;
        return traverse(root->left, root->val) && traverse(root->right, root->val);
    }

    bool isUnivalTree(TreeNode *root)
    {
        if (root == NULL)
            return true;
        return traverse(root, root->val);
    }
};