Given the root of a binary tree, return the length of the diameter of the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
The length of a path between two nodes is represented by the number of edges between them.
For example:
Consider the following binary tree:
1
/ \
2 3
/ \
4 5
The diameter of this tree is 3, which corresponds to the path [4,2,1,3] or [5,2,1,3].
As another example, consider the following binary tree:
1
/ \
2 3
The diameter of this tree is 2 because the longest path goes from one of the leaves, to the root, and then to the other leaf. Note that we want to return the number of edges and not nodes.
Write a function to calculate the diameter of a given binary tree.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The diameter of a tree is the longest path between any two nodes in the tree. A brute force approach involves checking every possible path to find the longest one. We essentially explore every single possible route through the tree.
Here's how the algorithm would work step-by-step:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def height_of_binary_tree(node):
if node is None:
return 0
left_height = height_of_binary_tree(node.left)
right_height = height_of_binary_tree(node.right)
return max(left_height, right_height) + 1
def diameter_of_binary_tree_brute_force(root):
if root is None:
return 0
# Calculate diameter with current node as root
left_tree_height = height_of_binary_tree(root.left)
right_tree_height = height_of_binary_tree(root.right)
diameter_with_root = left_tree_height + right_tree_height
# Recursively calculate diameter of left subtree
left_diameter = diameter_of_binary_tree_brute_force(root.left)
# Recursively calculate diameter of right subtree
right_diameter = diameter_of_binary_tree_brute_force(root.right)
# Return the maximum of the three diameters
return max(diameter_with_root, max(left_diameter, right_diameter))
The diameter of a binary tree is the longest path between any two nodes in the tree. The trick is to calculate the longest path passing through each node while also calculating the height of each node during a single exploration of the tree.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def diameter_of_binary_tree(root):
longest_path = 0
def depth_first_search(node):
nonlocal longest_path
# Base case: empty node has height 0
if not node:
return 0
left_height = depth_first_search(node.left)
right_height = depth_first_search(node.right)
# Update the diameter if needed.
longest_path = max(longest_path, left_height + right_height)
# The height of the current node
# is max child height + 1.
return max(left_height, right_height) + 1
depth_first_search(root)
return longest_path
Case | How to Handle |
---|---|
Null or empty tree | Return 0 as the diameter of an empty tree is defined as zero. |
Tree with only one node | Return 0, as the diameter of a single-node tree is zero since there are no paths. |
Tree with only left children (left-skewed) | The algorithm correctly calculates the height of the left subtree which will be the diameter. |
Tree with only right children (right-skewed) | The algorithm correctly calculates the height of the right subtree which will be the diameter. |
Balanced tree with many nodes (scalability) | Ensure recursive calls don't exceed stack limits by using an iterative solution or tail-call optimization if the language supports it. |
Tree with negative node values (if applicable, based on the problem's extended context/allowed values) | Node values are irrelevant to diameter calculation; the algorithm works based on node connections, not values. |
Integer overflow when calculating the height of subtrees (large tree) | Use a data type with a larger range (e.g., long) if the tree's height might cause an integer overflow. |
Tree where the diameter path does not pass through the root | The algorithm must correctly consider paths within subtrees, not just paths originating from the root node. |