LeetCode solutions

1005. Univalued Binary Tree

My accepted C++ solution to LeetCode problem 1005, Univalued Binary Tree, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 12.5MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 12.5MB, accepted 2025-12-27.

cpp
class Solution {
public:
    bool isUnivalTree(TreeNode* root) {
        if (!root) return true;
        if (root->left && root->left->val != root->val) return false;
        if (root->right && root->right->val != root->val) return false;
        return isUnivalTree(root->left) && isUnivalTree(root->right);
    }
};

Source