Home
DSA Patterns
Depth-First Search
Longest Univalue Path
Longest Univalue Path (LeetCode 687)
Longest Univalue Path (LeetCode 687): measure the longest path, in edges, along which every node holds one and the same value.
Examples
The longest path of value 5 is 5 -> 5 -> 5 (length 2).
Problem Understanding
We need the longest path (edges) where every node has the same value.
Like Diameter, the path does not need to pass through the root. It can form an inverted 'V' shape at any node.
Algorithm Strategy
We use Post-Order DFS.
At each node:
- Recursive calls get the longest univalue path extending down from left and right children.
- Check Value:
- If
node.leftexists andnode.left.val == node.val, we can extend the left path by 1. - Otherwise, the path from the left stops here (RESET to 0).
- Same logic for right child.
- If
- Update Global Max:
maxPath = max(maxPath, arrowLeft + arrowRight). - Return:
max(arrowLeft, arrowRight)(we can only extend one branch up to the parent).
Interactive Visualization
↑n = SAME-VALUE ARROW RETURNED
best = 0
best
0
Answer
:
call stack
No calls in progress
best = 0
Next
Call arrow on the root, 5
Tree
Under each finished node is ↑n, the same-value arrow it returned. A child with a different value gives 0, however long its own arrow was. The longest same-value path found so far is drawn in green.
Dry Run: [5, 4, 5, 1, 1, null, 5]
Tree: [5, 4, 5, 1, 1, null, 5]: root 5 has children 4 and 5; 4 has two children, both 1; the right 5 has a right child 5.
| Node | Left returns | Right returns | Left child same? | Right child same? | arrowLeft | arrowRight | best | Returns |
|---|---|---|---|---|---|---|---|---|
| 1 (left of 4) | 0 | 0 | no child | no child | 0 | 0 | 0 | 0 |
| 1 (right of 4) | 0 | 0 | no child | no child | 0 | 0 | 0 | 0 |
| 4 | 0 | 0 | 1 ≠ 4 | 1 ≠ 4 | 0 | 0 | 0 | 0 |
| 5 (leaf) | 0 | 0 | no child | no child | 0 | 0 | 0 | 0 |
| 5 (right of root) | 0 | 0 | no child | 5 = 5 | 0 | 1 | 1 | 1 |
| 5 (root) | 0 | 1 | 4 ≠ 5 | 5 = 5 | 0 | 2 | 2 | 2 |
Result: 2 edges, root 5 → right 5 → its right 5.
Edge Cases & Common Mistakes
- Resetting Path: If
node.val != child.val, the path length extending from that child MUST be 0. It's a common mistake to just passleftLenup. - Total Univalue Sum: We are finding path length (edges), not just counting nodes with same value.
- Global Max: Just like Diameter, the longest path might be wholly inside a subtree and never touch the root.
Solution
Post-order traversal with value check.
Longest Univalue Path Code
1var longestUnivaluePath = function(root) {2 let best = 0;34 function arrow(node) {5 if (!node) return 0;6 const left = arrow(node.left);7 const right = arrow(node.right);8 const arrowLeft = node.left && node.left.val === node.val ? left + 1 : 0;9 const arrowRight = node.right && node.right.val === node.val ? right + 1 : 0;10 best = Math.max(best, arrowLeft + arrowRight);11 return Math.max(arrowLeft, arrowRight);12 }1314 arrow(root);15 return best;16};
Longest Univalue Path in Python, Java, C++, TypeScript and C
Longest Univalue Path in Python
class Solution:
def longestUnivaluePath(self, root: Optional[TreeNode]) -> int:
self.best = 0
def arrow(node):
if not node:
return 0
left = arrow(node.left)
right = arrow(node.right)
arrow_left = left + 1 if node.left and node.left.val == node.val else 0
arrow_right = right + 1 if node.right and node.right.val == node.val else 0
self.best = max(self.best, arrow_left + arrow_right)
return max(arrow_left, arrow_right)
arrow(root)
return self.bestLongest Univalue Path in Java
class Solution {
private int best;
public int longestUnivaluePath(TreeNode root) {
best = 0;
arrow(root);
return best;
}
private int arrow(TreeNode node) {
if (node == null) return 0;
int left = arrow(node.left);
int right = arrow(node.right);
int arrowLeft = node.left != null && node.left.val == node.val ? left + 1 : 0;
int arrowRight = node.right != null && node.right.val == node.val ? right + 1 : 0;
best = Math.max(best, arrowLeft + arrowRight);
return Math.max(arrowLeft, arrowRight);
}
}Longest Univalue Path in C++
class Solution {
int best;
public:
int longestUnivaluePath(TreeNode* root) {
best = 0;
arrow(root);
return best;
}
int arrow(TreeNode* node) {
if (!node) return 0;
int left = arrow(node->left);
int right = arrow(node->right);
int arrowLeft = node->left && node->left->val == node->val ? left + 1 : 0;
int arrowRight = node->right && node->right->val == node->val ? right + 1 : 0;
best = max(best, arrowLeft + arrowRight);
return max(arrowLeft, arrowRight);
}
};Longest Univalue Path in TypeScript
function longestUnivaluePath(root: TreeNode | null): number {
let best = 0;
function arrow(node: TreeNode | null): number {
if (!node) return 0;
const left = arrow(node.left);
const right = arrow(node.right);
const arrowLeft = node.left && node.left.val === node.val ? left + 1 : 0;
const arrowRight = node.right && node.right.val === node.val ? right + 1 : 0;
best = Math.max(best, arrowLeft + arrowRight);
return Math.max(arrowLeft, arrowRight);
}
arrow(root);
return best;
}Longest Univalue Path in C
static int best;
static int arrow(struct TreeNode* node) {
if (node == NULL) return 0;
int left = arrow(node->left);
int right = arrow(node->right);
int arrowLeft = node->left && node->left->val == node->val ? left + 1 : 0;
int arrowRight = node->right && node->right->val == node->val ? right + 1 : 0;
if (arrowLeft + arrowRight > best) best = arrowLeft + arrowRight;
return arrowLeft > arrowRight ? arrowLeft : arrowRight;
}
int longestUnivaluePath(struct TreeNode* root) {
best = 0;
arrow(root);
return best;
}More practice: DSA interview questions with answers · patterns cheat sheet
