Simple tree with root and two children
Input
diameter_of_binary_tree(TreeNode(1, TreeNode(2), TreeNode(3)))
Output
3
Longest path is 2 -> 1 -> 3, which includes 3 nodes.
Full lesson preview
Compute the diameter (longest path) of a binary tree measured in nodes.
Problem statement
Task
Examples
Input
diameter_of_binary_tree(TreeNode(1, TreeNode(2), TreeNode(3)))
Output
3
Longest path is 2 -> 1 -> 3, which includes 3 nodes.
Input format
Output format
Constraints
Samples
Input
diameter_of_binary_tree(TreeNode(1))
Output
1
Single-node tree has diameter 1 (the node itself).