Find the Longest Edge Path in a Binary Tree

Quick Overview

Find the Longest Edge Path in a Binary Tree. Return the diameter of the binary tree rooted at root: the maximum number of edges on a path joining any two nodes.

Find the Longest Edge Path in a Binary Tree

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return the diameter of the binary tree rooted at root: the maximum number of edges on a path joining any two nodes. The path may pass through a node other than the root. TreeNode has val, left and right fields. ```cpp class Solution { public: int diameterOfBinaryTree(TreeNode* root); }; ``` ### Examples **Example 1:** ```text root = [9,5,12,3,null,null,15,1] Output: 5 Explanation: 1 -> 3 -> 5 -> 9 -> 12 -> 15 has five edges. ``` **Example 2:** ```text root = [4] Output: 0 Explanation: A singleton has no edge. ``` ### Constraints - The binary tree contains 1..10000 nodes, with values from -100 through 100. ### Clarifying Questions Count edges rather than nodes and allow the maximizing path to remain inside one subtree. ```hint Read the boundary condition Which two subtree measurements determine the best path whose highest node is the current node? ``` ### Contract considerations Count edges rather than nodes and allow the maximizing path to remain inside one subtree.

Overview: Find the Longest Edge Path in a Binary Tree. Return the diameter of the binary tree rooted at root: the maximum number of edges on a path joining any two nodes.

Roblox logo
Roblox
Sep 14, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the diameter of the binary tree rooted at root: the maximum number of edges on a path joining any two nodes. The path may pass through a node other than the root. TreeNode has val, left and right fields.

class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root);
};

Examples

Example 1:

root = [9,5,12,3,null,null,15,1]
Output: 5
Explanation: 1 -> 3 -> 5 -> 9 -> 12 -> 15 has five edges.

Example 2:

root = [4]
Output: 0
Explanation: A singleton has no edge.

Constraints

  • The binary tree contains 1..10000 nodes, with values from -100 through 100.

Clarifying Questions Guidance

Count edges rather than nodes and allow the maximizing path to remain inside one subtree.

Contract considerations

Count edges rather than nodes and allow the maximizing path to remain inside one subtree.