A binary tree is considered uni-valued if every node in the tree contains the same value.
Given the root of a binary tree, return true if the tree is uni-valued, or false otherwise.
Constraints:
The number of nodes in the tree is in the range
Node.data
The key intuition behind this solution is that a binary tree is uni-valued if and only if every parent node has the same value as its children. We can verify this property using a depth-first search (DFS) traversal. At each node, we compare its value with the values of its immediate children (if they exist). If any child’s value differs from the current node’s value, we immediately return FALSE. Otherwise, we recursively check both the left and right subtrees. If all comparisons pass throughout the entire tree, the tree is uni-valued, and we return TRUE.
Now, let’s look at the solution steps below: ...
A binary tree is considered uni-valued if every node in the tree contains the same value.
Given the root of a binary tree, return true if the tree is uni-valued, or false otherwise.
Constraints:
The number of nodes in the tree is in the range
Node.data
The key intuition behind this solution is that a binary tree is uni-valued if and only if every parent node has the same value as its children. We can verify this property using a depth-first search (DFS) traversal. At each node, we compare its value with the values of its immediate children (if they exist). If any child’s value differs from the current node’s value, we immediately return FALSE. Otherwise, we recursively check both the left and right subtrees. If all comparisons pass throughout the entire tree, the tree is uni-valued, and we return TRUE.
Now, let’s look at the solution steps below: ...