Longest downward path from the root of a binary tree with all distinct values

Quick Overview

Given a binary tree, find the length of the longest path that starts at the root, moves downward and never repeats a node value. It tests depth-first traversal with backtracking state, and handling trees deep enough to overflow a recursive call stack.

Longest downward path from the root of a binary tree with all distinct values

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree, return the length of the longest path starting from the root and going downward such that all node values along the path are distinct. A path is a sequence of nodes from the root following parent-to-child connections. The length of the path is the number of nodes in it. The path must start at the root, but it does not need to end at a leaf: it can stop at any node. Values only need to be distinct along one path, so the same value may appear in different branches. ### Function Signature ```python def longest_distinct_path(root: TreeNode) -> int: ``` ### Rules - `root` is a `TreeNode` with fields `val`, `left` and `right`. A missing child is `None`. - Examples write the tree as a level-order list. `None` marks a missing child, and the children of a missing node are not listed. ### Constraints - The number of nodes in the tree is in the range `[1, 10^4]`. - `-10^5 <= Node.val <= 10^5` ### Examples ```text Input: root = [1, 2, 3, 4, 2, 5, 1] Output: 3 ``` The root 1 has children 2 and 3, node 2 has children 4 and 2, and node 3 has children 5 and 1. The paths `1 -> 2 -> 4` and `1 -> 3 -> 5` have three distinct values, while `1 -> 2 -> 2` and `1 -> 3 -> 1` repeat a value at their third node. ```text Input: root = [1, 1] Output: 1 ``` The only child repeats the root's value 1, so the longest distinct path is the root alone. ```text Input: root = [7, 4, 9, None, 7, 4, None, None, 1, 2] Output: 4 ``` The root 7 has children 4 and 9. Node 4 has a right child 7 whose right child is 1, and node 9 has a left child 4 whose left child is 2. `7 -> 9 -> 4 -> 2` has four distinct values (the 4 under node 9 only shares its value with a node in another branch), while the left branch repeats 7 at its third node, so its longest valid part is `7 -> 4`.

Overview: Given a binary tree, find the length of the longest path that starts at the root, moves downward and never repeats a node value. It tests depth-first traversal with backtracking state, and handling trees deep enough to overflow a recursive call stack.

Grab logo
Grab
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return the length of the longest path starting from the root and going downward such that all node values along the path are distinct.

A path is a sequence of nodes from the root following parent-to-child connections. The length of the path is the number of nodes in it. The path must start at the root, but it does not need to end at a leaf: it can stop at any node. Values only need to be distinct along one path, so the same value may appear in different branches.

Function Signature

def longest_distinct_path(root: TreeNode) -> int:

Rules

  • root is a TreeNode with fields val , left and right . A missing child is None .
  • Examples write the tree as a level-order list. None marks a missing child, and the children of a missing node are not listed.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4] .
  • -10^5 <= Node.val <= 10^5

Examples

Input: root = [1, 2, 3, 4, 2, 5, 1]
Output: 3

The root 1 has children 2 and 3, node 2 has children 4 and 2, and node 3 has children 5 and 1. The paths 1 -> 2 -> 4 and 1 -> 3 -> 5 have three distinct values, while 1 -> 2 -> 2 and 1 -> 3 -> 1 repeat a value at their third node.

Input: root = [1, 1]
Output: 1

The only child repeats the root's value 1, so the longest distinct path is the root alone.

Input: root = [7, 4, 9, None, 7, 4, None, None, 1, 2]
Output: 4

The root 7 has children 4 and 9. Node 4 has a right child 7 whose right child is 1, and node 9 has a left child 4 whose left child is 2. 7 -> 9 -> 4 -> 2 has four distinct values (the 4 under node 9 only shares its value with a node in another branch), while the left branch repeats 7 at its third node, so its longest valid part is 7 -> 4.