Longest increasing downward path in a binary tree
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's technical phone screen, last asked Nov 2025. It tests binary tree and dfs.
Given a binary tree of integers, find the longest path that runs from some node down to one of its descendants, where every child on the path has a strictly greater value than its parent. Return the path itself, as the list of values from top to bottom.
Interface
Unless you agree on a different interface with the interviewer, assume:
class TreeNode: val: int; left: TreeNode | None; right: TreeNode | None-
longest_increasing_path(root: TreeNode | None) -> list[int]— the values along one longest path. Any longest path is accepted. An empty tree returns[].
Example
5
/ \
6 2
/ / \
1 3 8
/ \
4 0
longest_increasing_path(root) -> [2, 3, 4]
5 -> 6 and 2 -> 8 have two nodes each. 5 -> 2 is not increasing, so the path cannot start at the root.
Constraints
- Up to
2 * 10^5nodes. Values may repeat; equal values break a path. - The path starts at any node, not only the root, and goes only downward.
Clarifying questions
- Strictly increasing, or is equal allowed?
- Must the values increase by exactly 1, or by any amount?
- If several paths tie, may I return any of them?
- Is the tree deep enough that recursion could overflow?
What a strong answer covers
- A post-order walk that returns, for each node, the longest increasing path starting at it. A child's path extends the parent's only when
child.val > parent.val. - Keeping the path itself: store the best child per node (or its length) and rebuild the path from the best start, rather than copying lists at every node.
- O(n) time, O(h) stack for a tree of height
h, or an explicit stack for very deep trees. - Tests: an empty tree, one node, a strictly decreasing chain, ties, equal values.
Follow-ups
- Values must increase by exactly 1 (consecutive sequence).
- The path may go up and then down through a node, increasing in both directions from the lowest point.
- Return every longest path.
Where it's asked
- Meta · Technical phone screen · Last asked Nov 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
What it tests
binary tree · dfs
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
