Zepruv › Coding problems

Univalued Binary Tree

Easy · Tree, Depth-First Search, Breadth-First Search, Binary Tree

A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.

Example 1

Input: root = [1,1,1,1,1,null,1]
Output: true

Example 2

Input: root = [2,2,2,5,2]
Output: false

Constraints

Starter code (python)

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isUnivalTree(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: bool
        """
        

Related problems

Previous: Minimum Area Rectangle II · Next: Numbers With Same Consecutive Differences · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.