Leetcode 1005 Solution

This article provides solution to leetcode question 1005 (univalued-binary-tree)

https://leetcode.com/problems/univalued-binary-tree

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isUnivalTree(TreeNode* root) {
        TreeNode* left = root->left;
        TreeNode* right = root->right;

        if (left && (left->val != root->val || isUnivalTree(left) == false))
            return false;

        if (right && (right->val != root->val || isUnivalTree(right) == false))
            return false;

        return true;
    }
};