Univalued Binary Tree (LeetCode 965): Solution and Explanation
Given a binary tree, return true when every node holds the same value and false as soon as any node holds a different one.
Intuition
If all the nodes share one value, that value has to be the root's value. So there is no guessing: read the root and you know exactly what every other node must hold.
After that the problem is a plain traversal. Visit each node once and compare it with the target. The first node that differs settles the answer, so there is no reason to keep walking after it.
Approach: Iterative DFS against the root value
- Store target = root.val and push the root onto a stack.
- While the stack is not empty, pop a node.
- If the node's value is not the target, return false.
- Push its right child, then its left child, so the left side is checked first.
- When the stack runs empty, every node matched: return true.
Step-by-step example
root = [1,1,1,1,1,null,1]
- The root holds 1, so target = 1. Push the root.
- Pop the root: 1 equals 1. Push its right child, then its left child.
- Pop the left child (1), which pushes its two leaves; pop both leaves, each equal to 1.
- Pop the right child (1), which pushes its right leaf; pop that leaf, also 1.
- The stack is empty after six matching nodes, so return true.
Code: Python, Java, C++
1class Solution:
2 def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
3 target = root.val
4 stack = [root]
5 while stack:
6 node = stack.pop()
7 if node.val != target:
8 return False
9 if node.right:
10 stack.append(node.right)
11 if node.left:
12 stack.append(node.left)
13 return True1class Solution {
2 public boolean isUnivalTree(TreeNode root) {
3 int target = root.val;
4 Deque<TreeNode> stack = new ArrayDeque<>();
5 stack.push(root);
6 while (!stack.isEmpty()) {
7 TreeNode node = stack.pop();
8 if (node.val != target) return false;
9 if (node.right != null) stack.push(node.right);
10 if (node.left != null) stack.push(node.left);
11 }
12 return true;
13 }
14}1class Solution {
2public:
3 bool isUnivalTree(TreeNode* root) {
4 int target = root->val;
5 vector<TreeNode*> stack = {root};
6 while (!stack.empty()) {
7 TreeNode* node = stack.back();
8 stack.pop_back();
9 if (node->val != target) return false;
10 if (node->right) stack.push_back(node->right);
11 if (node->left) stack.push_back(node->left);
12 }
13 return true;
14 }
15};Time and space complexity
O(n)
Each node is pushed and popped at most once, with one comparison each.
O(h)
The stack holds at most about one root-to-leaf path plus the siblings waiting beside it, which is bounded by the height for a DFS.
Other approaches
Recursive DFS (time O(n), space O(h))
Return true for a null node, false if the node differs from the root value, otherwise recurse into both children and combine with and.
Compare with the parent (time O(n), space O(h))
Check that every child equals its parent. If every edge joins equal values, the whole connected tree holds one value.
Common mistakes
- Collecting all values into a set before deciding. It works, but it never stops early and uses O(n) extra memory.
- Pushing null children and then reading .val on them. Only push children that exist.
- Comparing each node only with its sibling. Siblings can agree with each other and still differ from the root.
Frequently asked questions
Why compare with the root instead of the parent?
Both work. Comparing with the root is a single fixed target, which is simpler to read and lets the traversal be in any order.
Can the answer be decided before visiting every node?
A false answer can: the first differing node ends the search. A true answer needs every node checked, because any unvisited node might differ.
Does the traversal order matter?
No. DFS, BFS or any order visits the same set of nodes. The order only changes which mismatch is found first.