FastPrepDiameter of a Binary Tree
Problems/Diameter of a Binary Tree

Diameter of a Binary Tree

Adobe logoAdobe● EasyINTERNFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem measures the longest path between any two nodes of a binary tree. Three Adobe first-round reports ask for it. A report by a product intern hired through a 2024 on-campus drive (posted August 2024, selected) reached it after a question about which data structure the author liked best. It counts the path in nodes. The author's first, slower idea had to be improved, including its memory use. A report from a candidate who applied through a job listing with an employee referral (posted August 2023) has it as the one coding task of the opening technical round, between resume and project questions. An SDE intern entry (interview in October 2022, rejected) lists it among five items of a single 50-minute video round. None of them gives limits or examples.

Our choices: the length is counted in edges, so a single node gives 0. The 2024 intern report's node count is this answer plus one, and the editorial shows that change. Trees have 1 to 500 nodes, with values from -1000 to 1000. The examples are new.

Our version is about 85% the same as the reported questions.

Given the root of a binary tree, return the length of its longest path, measured as the number of edges between the two end nodes. A path moves along parent-child links and never uses a node twice.

The longest path does not have to pass through the root. A tree with a single node has a longest path of length 0.

Function

diameterLength(root: TreeNode) → int

Examples

Example 1

root = [1,2,null,4,5,6,null,null,7,8]return = 5
root
1246857

The longest path is 8, 6, 4, 2, 5, 7: five edges. It does not pass through the root 1; the longest path that does (8, 6, 4, 2, 1) has only four edges.

Example 2

root = [7]return = 0
root
7

A single node: the only path has no edges, so the answer is 0.

Example 3

root = [10,20,30,40,null,null,50]return = 4
root
1020403050

The longest path is 40, 20, 10, 30, 50, which goes through the root: four edges.

Constraints

  • The tree has between 1 and 500 nodes.
  • -1000 <= node.val <= 1000
  • The tree does not have to be balanced. It can be a single chain of 500 nodes.

More Adobe Phone Screen questions

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
  • Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
  • Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
  • Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
  • Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
  • Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • Sum All Values in a Binary TreeDuolingo · PHONE SCREENDuolingo interview questions
  • Sum Root-to-Leaf Binary NumbersOracle · PHONE SCREENOracle interview questions
  • Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
  • Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
  • Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
  • Binary Tree Preorder TraversalMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
  • Range Sum of BSTMeta · PHONE SCREENMeta interview questions
  • Invert Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions

Interview reports that mention this question

  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)
  • Adobe SWE Intern Interviews: 27 Experiences (Part 2 of 4)
  • Adobe Intern Interview Questions: 54 Experiences (Part 1 of 4)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
All Adobe interview questionsSee Adobe hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int diameterLength(TreeNode root) {
        // Write your code here.
    }
}
Checking account…