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:
[1, 100]
.0 <= Node.val < 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to checking if a tree is univalued involves looking at every single node in the tree. We essentially compare each node's value with the value of the root node to make sure they all match.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def is_univalued_binary_tree_brute_force(root):
if not root:
return True
root_value = root.value
def is_univalued(node):
if not node:
return True
# If current node's value differs, tree is not univalued.
if node.value != root_value:
return False
# Recursively check the left and right subtrees
return is_univalued(node.left) and is_univalued(node.right)
return is_univalued(root)
The task is to check if all nodes in a tree have the same value. The fastest way is to start at the root and then check if all the other nodes match the root's value. We can do this recursively.
Here's how the algorithm would work step-by-step:
def is_unival_tree(root):
if not root:
return True
root_value = root.val
def is_unival(node, value):
if not node:
return True
if node.val != value:
return False
return is_unival(node.left, value) and is_unival(node.right, value)
# Check if the left subtree exists
if root.left:
if not is_unival(root.left, root_value):
return False
# Check if the right subtree exists
if root.right:
if not is_unival(root.right, root_value):
return False
return True
Case | How to Handle |
---|---|
Null root node | Return true because an empty tree is trivially univalued. |
Single node tree | Return true, as a single node tree is always univalued. |
Tree with all nodes having the same value | The algorithm should correctly return true since all nodes have equal values. |
Tree with a different value at the root but all other nodes matching | The algorithm should return false as the root value differs from other nodes. |
Deeply skewed tree (e.g., only left or right children) | Ensure the recursive calls do not cause a stack overflow for extreme depths. |
Tree with a large number of nodes | Ensure the algorithm scales efficiently (O(n)) and doesn't have unnecessary overhead. |
Integer overflow in node values (if applicable depending on language) | Consider using a larger data type if the problem specifies a range that could cause overflow. |
Values in left subtree are equal, but the values in right subtree differ from left subtree's value | The algorithm should correctly return false since not all nodes have equal values. |