Diameter of a Binary Tree
Updated
Problem statement
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) → intExamples
Example 1
root = [1,2,null,4,5,6,null,null,7,8]return = 5The 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 = 0A single node: the only path has no edges, so the answer is 0.
Example 3
root = [10,20,30,40,null,null,50]return = 4The longest path is 40, 20, 10, 30, 50, which goes through the root: four edges.
Constraints
- The tree has between
1and500nodes. -1000 <= node.val <= 1000- The tree does not have to be balanced. It can be a single chain of
500nodes.
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