Diameter of a Binary Tree
Updated
Problem statement
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) → intExamples
Example 1
root = [1,2,3,4,5]return = 3A longest path is 4 → 2 → 1 → 3, which contains three edges.
Example 2
root = [1,2]return = 1The only nonempty path between distinct nodes uses one edge.
Example 3
root = []return = 0An empty tree has no edges.
Constraints
- The tree contains between
0and100000nodes. -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)