Univalued Binary Tree
Easy
Topics
A binary tree is univalued if every node has the same value. Given the root, return true if the tree is univalued, otherwise false.
Example 1
true. Each node only needs comparing with its own parent — if that holds everywhere, it holds against the root by transitivity.null marks a missing child.Input: root = [1,1,1,1,1,null,1] Output: true
Example 2
false. One mismatch anywhere is enough, so the traversal can stop there.Input: root = [2,2,2,5,2] Output: false
Constraints
- 1 <= number of nodes <= 100
- 0 <= Node.val <= 99
Editorial — intuition, approach, complexity and pitfalls
Intuition
Every node must hold the same value, and the root's value is as good a reference as any. So walk the tree and compare each node against it, short-circuiting on the first mismatch.
Comparing against the root rather than against each node's parent is the simpler framing — equality is transitive, so "all equal to the root" and "every node equals its parent" are the same condition, but the first needs no state threaded through the recursion.
Approach
- DFS. A null node is vacuously fine.
- If the node's value differs from the root's, return false.
- Otherwise require both subtrees to pass.
The and short-circuits, so the right subtree is not explored once the left has failed.
Dry Run
tree 1 tree 2
/ \ / \
1 1 2 2
/ \ /
1 1 5
every node == 1 -> true node 5 != 2 -> false,
and the recursion stops thereComplexity
- Time — O(n) worst case, and less when a mismatch appears early.
- Space — O(h): the recursion stack, up to O(n) on a degenerate tree. An iterative stack or queue would be O(w) in the width instead, which is better on a deep thin tree and worse on a bushy one.
Common Pitfalls
- Returning false at a null node. Null is not a violation; the base case must be true.
- Comparing each node only against its immediate children and forgetting that a single-child node still needs checking.
- Using
&rather thanandin languages where that forces both sides to evaluate, losing the short-circuit. - Capturing the reference value from somewhere other than the root in a way that differs per call — the comparison target must be fixed.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def isUnivalTree(self, root):
def dfs(node):
if not node: return True
if node.val != root.val: return False
return dfs(node.left) and dfs(node.right)
return dfs(root)import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public boolean isUnivalTree(TreeNode root) {
rootVal = root.val;
return dfs(root);
}
int rootVal;
private boolean dfs(TreeNode node) {
if (node == null) return true;
if (node.val != rootVal) return false;
return dfs(node.left) && dfs(node.right);
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: bool isUnivalTree(TreeNode* root) {
int rv = root->val;
function<bool(TreeNode*)> dfs = [&](TreeNode* node) -> bool {
if (!node) return true;
if (node->val != rv) return false;
return dfs(node->left) && dfs(node->right);
};
return dfs(root);
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {boolean}
*/ var isUnivalTree = function(root) {
const dfs = (node) => {
if (!node) return true;
if (node.val !== root.val) return false;
return dfs(node.left) && dfs(node.right);
};
return dfs(root);
};