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.
Example 1:
Input: root = [1,2,3,4,5] Output: 3 Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:
Input: root = [1,2] Output: 1
Constraints:
[1, 104]
.-100 <= Node.val <= 100
program main
use, intrinsic :: iso_fortran_env, only : error_unit, iostat_end, DP => REAL64
implicit none
integer, parameter :: unit_in = 10, unit_out = 20
character(len=1024) :: line
type :: node
real(kind=DP), allocatable :: val(:)
class(node), pointer :: left, right
end type node
type(node), pointer :: root
integer :: n, i, j, status
! read input
open(newunit=unit_in, file='input.txt', status='old', action='read', iostat=status)
if (status /= 0) then
write(error_unit, '(*(G0))') 'error: could not open input file'
stop 1
end if
read(unit=unit_in, fmt=*, iostat=status) n
allocate(root%val(n))
do i = 1, n
read(unit=unit_in, fmt=*, iostat=status) root%val(i)
if (status /= 0) exit
end do
close(unit=unit_in)
! solve problem
call solve(root)
! output solution
open(newunit=unit_out, file='output.txt', status='replace', action='write', iostat=status)
if (status /= 0) then
write(error_unit, '(*(G0))') 'error: could not open output file'
stop 1
end if
write(unit=unit_out, fmt='(I0)') diameter(root)
close(unit=unit_out)
contains
subroutine solve(root)
class(node), intent(inout) :: root
integer :: i
! TODO: solve the problem here
end subroutine solve
function diameter(root) result(res)
class(node), intent(in) :: root
integer :: res
! TODO: compute the diameter of the tree
end function diameter
end program main
temp.f95:14:17: 14 | open(newunit=unit_in, file='input.txt', status='old', action='read', iostat=status) | 1 Error: Named constant ‘unit_in’ in variable definition context (NEWUNIT tag) at (1) temp.f95:31:17: 31 | open(newunit=unit_out, file='output.txt', status='replace', action='write', iostat=status) | 1 Error: Named constant ‘unit_out’ in variable definition context (NEWUNIT tag) at (1)
module binary_tree_diameter
implicit none
private
public :: binary_tree_diameter
type :: binary_tree_node
integer :: val
type(binary_tree_node), pointer :: left
type(binary_tree_node), pointer :: right
end type binary_tree_node
interface binary_tree_diameter
module procedure :: binary_tree_diameter_recursive
end interface
contains
function binary_tree_diameter_recursive(root) result(diameter)
type(binary_tree_node), pointer, intent(in) :: root
integer :: diameter
if (.not. associated(root)) then
diameter = 0
return
end if
! Recursively compute the diameter of the left and right subtrees
! and store the maximum of these values
diameter = max(binary_tree_diameter_recursive(root%left), binary_tree_diameter_recursive(root%right))
! Add the length of the path from the root to the leaf nodes
! to the maximum diameter
diameter = max(diameter, binary_tree_depth(root) + binary_tree_depth(root%left) + binary_tree_depth(root%right))
end function binary_tree_diameter_recursive
function binary_tree_depth(root) result(depth)
type(binary_tree_node), pointer, intent(in) :: root
integer :: depth
if (.not. associated(root)) then
depth = 0
return
end if
! Recursively compute the depth of the left and right subtrees
! and store the maximum of these values
depth = max(binary_tree_depth(root%left), binary_tree_depth(root%right)) + 1
end function binary_tree_depth
end module binary_tree_diameter
program main
use binary_tree_diameter
implicit none
! Create a binary tree with the given values
type(binary_tree_node), pointer :: root
root => binary_tree_node(1, null(), null())
root%left => binary_tree_node(2, null(), null())
root%right => binary_tree_node(3, null(), null())
root%left%left => binary_tree_node(4, null(), null())
root%left%right => binary_tree_node(5, null(), null())
! Print the diameter of the tree
write (*,*) binary_tree_diameter(root)
end program main
temp.f95:6:30: 6 | public :: binary_tree_diameter | 1 Error: PUBLIC attribute applied to MODULE binary_tree_diameter at (1) temp.f95:14:30: 14 | interface binary_tree_diameter | 1 Error: MODULE attribute of ‘binary_tree_diameter’ conflicts with PROCEDURE attribute at (1) temp.f95:15:21: 15 | module procedure :: binary_tree_diameter_recursive | 1 Error: MODULE PROCEDURE at (1) must be in a generic module interface temp.f95:16:3: 16 | end interface | 1 Error: Expecting END MODULE statement at (1) temp.f95:49:16: 49 | depth = max(binary_tree_depth(root%left), binary_tree_depth(root%right)) + 1 | 1 Error: Function ‘binary_tree_depth’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:31:19: 31 | diameter = max(binary_tree_diameter_recursive(root%left), binary_tree_diameter_recursive(root%right)) | 1 Error: Function ‘binary_tree_diameter_recursive’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:55:9: 55 | use binary_tree_diameter | 1 Fatal Error: Cannot open module file ‘binary_tree_diameter.mod’ for reading at (1): No such file or directory compilation terminated.
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):
def _diameter_of_binary_tree(node):
if not node:
return 0, 0
left_height, left_diameter = _diameter_of_binary_tree(node.left)
right_height, right_diameter = _diameter_of_binary_tree(node.right)
height = 1 + max(left_height, right_height)
diameter = max(left_height + right_height, max(left_diameter, right_diameter))
return height, diameter
return _diameter_of_binary_tree(root)[1]
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
int diameterOfBinaryTree(TreeNode* root, int* height) {
int leftHeight = 0, rightHeight = 0;
if (root == nullptr) {
*height = 0;
return 0;
}
int leftDiameter = diameterOfBinaryTree(root->left, &leftHeight);
int rightDiameter = diameterOfBinaryTree(root->right, &rightHeight);
*height = std::max(leftHeight, rightHeight) + 1;
return std::max(std::max(leftDiameter, rightDiameter), leftHeight + rightHeight);
}