Find the longest path between any two nodes in a binary tree

Quick Overview

Given the root of a binary tree, find the number of edges on the longest path between any two nodes, where the path may or may not pass through the root. It tests depth-first traversal of trees, computing more than one value per subtree in a single pass, and handling deep unbalanced trees.

Find the longest path between any two nodes in a binary tree

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the root of a binary tree, return the length of the diameter of the tree. The diameter is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root. The length of a path is the number of edges between its nodes. ### Function Signature ```python def diameter_of_binary_tree(root: TreeNode) -> int: ... ``` ### Rules - `root` is a `TreeNode` with attributes `val`, `left` and `right`. A missing child is `None`. - Examples write the tree as a level-order list. `None` marks a missing child, the children of a missing node are not listed, and trailing `None` values are dropped. - Path length counts edges. A path that visits $k$ nodes has length $k - 1$, so a tree with one node has diameter 0. ### Constraints - The number of nodes in the tree is between 1 and 10,000. - `-100 <= Node.val <= 100` - The tree does not have to be balanced. Its height can be as large as the number of nodes. ### Examples ```text Input: root = [1, 2, 3, 4, 5] Output: 3 ``` Node 1 has children 2 and 3, and node 2 has children 4 and 5. The path 4, 2, 1, 3 has 3 edges (so does 5, 2, 1, 3), and no path is longer. ```text Input: root = [1, 2] Output: 1 ``` The tree has a single edge, between node 1 and its left child 2, so the longest path is that edge. ```text Input: root = [1, 2, None, 3, 4, 5, None, None, 6] Output: 4 ``` Node 1 has only a left child, 2. Node 2 has children 3 and 4, node 3 has a left child 5, and node 4 has a right child 6. The path 5, 3, 2, 4, 6 has 4 edges and never reaches the root. The longest path through the root, 1, 2, 3, 5, has only 3.

Overview: Given the root of a binary tree, find the number of edges on the longest path between any two nodes, where the path may or may not pass through the root. It tests depth-first traversal of trees, computing more than one value per subtree in a single pass, and handling deep unbalanced trees.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return the length of the diameter of the tree. The diameter is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root. The length of a path is the number of edges between its nodes.

Function Signature

def diameter_of_binary_tree(root: TreeNode) -> int: ...

Rules

  • root is a TreeNode with attributes val , left and right . A missing child is None .
  • Examples write the tree as a level-order list. None marks a missing child, the children of a missing node are not listed, and trailing None values are dropped.
  • Path length counts edges. A path that visits kk nodes has length k−1k - 1 , so a tree with one node has diameter 0.

Constraints

  • The number of nodes in the tree is between 1 and 10,000.
  • -100 <= Node.val <= 100
  • The tree does not have to be balanced. Its height can be as large as the number of nodes.

Examples

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

Node 1 has children 2 and 3, and node 2 has children 4 and 5. The path 4, 2, 1, 3 has 3 edges (so does 5, 2, 1, 3), and no path is longer.

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

The tree has a single edge, between node 1 and its left child 2, so the longest path is that edge.

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

Node 1 has only a left child, 2. Node 2 has children 3 and 4, node 3 has a left child 5, and node 4 has a right child 6. The path 5, 3, 2, 4, 6 has 4 edges and never reaches the root. The longest path through the root, 1, 2, 3, 5, has only 3.