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

Diameter of a Binary Tree

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Bloomberg new grad onsite interview. The report names the well-known Diameter of Binary Tree problem, number 543, and says to return the longest length as a number of edges, not nodes. Our version keeps that standard rule.

Given the root of a binary tree, return its diameter: the maximum number of edges on a path between any two nodes.

The path may or may not pass through the root. Return 0 for an empty tree or a one-node tree.

Function

diameterOfBinaryTree(root: TreeNode) → int

Examples

Example 1

root = [1,2,3,4,5]return = 3
root
12453

A longest path is 4 → 2 → 1 → 3, which contains three edges.

Example 2

root = [1,2]return = 1
root
12

The only nonempty path between distinct nodes uses one edge.

Example 3

root = []return = 0
root
empty

An empty tree has no edges.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -10^9 <= Node.val <= 10^9.
  • Node values do not affect the answer.

More Bloomberg LP Onsite Interview questions

  • Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Diameter of Binary TreeeBay · ONSITE INTERVIEWeBay 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
  • Path SumSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Diameter of a Binary TreeMeta · ONSITE INTERVIEWMeta interview questions
  • Check That Every Node Equals Its Descendant SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions

Bloomberg LP interview experiences

  • Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
  • Bloomberg SWE Intern Interview Questions: 68 Questions
  • Bloomberg Interview Questions, Level Not Reported: 65 Questions
  • Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
  • Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
  • Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)
All Bloomberg LP interview questionsSee Bloomberg LP 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 diameterOfBinaryTree(TreeNode root) {
        // Write your code here.
    }

}
Checking account…