Given the root
of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: 3
Example 2:
Input: root = [1,null,2] Output: 2
Constraints:
[0, 104]
.-100 <= Node.val <= 100
program max_depth
use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
implicit none
type, public :: node_t
real(kind=DP) :: val
class(node_t), pointer :: left => null()
class(node_t), pointer :: right => null()
end type
interface
function max_depth_recursive(root) result(res)
import :: node_t
class(node_t), intent(in) :: root
integer :: res
end function
function max_depth_iterative(root) result(res)
import :: node_t
class(node_t), intent(in) :: root
integer :: res
end function
end interface
type(node_t), target :: root
integer :: res
call test_max_depth(root)
contains
subroutine test_max_depth(root)
type(node_t), intent(inout) :: root
write (unit=error_unit, fmt='(A)') 'Testing recursive solution...'
res = max_depth_recursive(root)
if (res /= 3) then
write (unit=error_unit, fmt='(A)') 'Recursive solution failed!'
stop 1
else
write (unit=error_unit, fmt='(A)') 'Recursive solution passed.'
end if
write (unit=error_unit, fmt='(A)') 'Testing iterative solution...'
res = max_depth_iterative(root)
if (res /= 3) then
write (unit=error_unit, fmt='(A)') 'Iterative solution failed!'
stop 1
else
write (unit=error_unit, fmt='(A)') 'Iterative solution passed.'
end if
end subroutine
function max_depth_recursive(root) result(res)
class(node_t), intent(in) :: root
integer :: res
if (.not. associated(root)) then
res = 0
else
res = 1 + max(max_depth_recursive(root%left), max_depth_recursive(root%right))
end if
end function
function max_depth_iterative(root) result(res)
class(node_t), intent(in) :: root
integer :: res
type(node_t), pointer :: current => root
integer :: level, max_level
do while (associated(current))
level = 0
do while (associated(current))
level = level + 1
current => current%left
end do
max_level = max(max_level, level)
current => root
level = 0
do while (associated(current))
level = level + 1
current => current%right
end do
max_level = max(max_level, level)
end do
res = max_level
end function
end program
temp.f95:5:17: 5 | type, public :: node_t | 1 Error: Derived type at (1) can only be PUBLIC in the specification part of a module temp.f95:7:22: 7 | class(node_t), pointer :: left => null() | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:8:22: 8 | class(node_t), pointer :: right => null() | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:9:7: 9 | end type | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:13:28: 13 | import :: node_t | 1 Error: Cannot IMPORT ‘node_t’ from host scoping unit at (1) - does not exist. temp.f95:14:26: 14 | class(node_t), intent(in) :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:19:28: 19 | import :: node_t | 1 Error: Cannot IMPORT ‘node_t’ from host scoping unit at (1) - does not exist. temp.f95:20:26: 20 | class(node_t), intent(in) :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:25:17: 25 | type(node_t), target :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:33:21: 33 | type(node_t), intent(inout) :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:54:38: 54 | function max_depth_recursive(root) result(res) | 1 Error: Symbol ‘max_depth_recursive’ at (1) already has an explicit interface temp.f95:55:22: 55 | class(node_t), intent(in) :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:56:22: 56 | integer :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:58:40: 58 | if (.not. associated(root)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:59:19: 59 | res = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:60:12: 60 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:61:52: 61 | res = 1 + max(max_depth_recursive(root%left), max_depth_recursive(root%right)) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:62:11: 62 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:63:7: 63 | end function | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:65:38: 65 | function max_depth_iterative(root) result(res) | 1 Error: Symbol ‘max_depth_iterative’ at (1) already has an explicit interface temp.f95:66:22: 66 | class(node_t), intent(in) :: root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:67:22: 67 | integer :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:69:21: 69 | type(node_t), pointer :: current => root | 1 Error: Derived type ‘node_t’ at (1) is being used before it is defined temp.f95:70:35: 70 | integer :: level, max_level | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:72:38: 72 | do while (associated(current)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:73:21: 73 | level = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:74:42: 74 | do while (associated(current)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:75:33: 75 | level = level + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:76:36: 76 | current => current%left | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:77:15: 77 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:78:45: 78 | max_level = max(max_level, level) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:79:27: 79 | current => root | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:80:21: 80 | level = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:81:42: 81 | do while (associated(current)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:82:33: 82 | level = level + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:83:36: 83 | current => current%right | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:84:15: 84 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:85:45: 85 | max_level = max(max_level, level) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:86:11: 86 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:88:23: 88 | res = max_level | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:89:7: 89 | end function | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:32:34: 32 | subroutine test_max_depth(root) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:28:28: 28 | call test_max_depth(root) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type
module BinaryTree
implicit none
type :: Node
integer :: val
type(Node), pointer :: left
type(Node), pointer :: right
end type Node
interface
function max_depth(root) result(depth)
type(Node), pointer, intent(in) :: root
integer :: depth
end function max_depth
end interface
contains
function max_depth(root) result(depth)
type(Node), pointer, intent(in) :: root
integer :: depth
if (associated(root)) then
depth = max(max_depth(root%left), max_depth(root%right)) + 1
else
depth = 0
end if
end function max_depth
end module BinaryTree
program main
use BinaryTree
implicit none
type(Node), pointer :: root
integer :: depth
! Example 1
root => Node(val=3, left=Node(val=9), right=Node(val=20, left=Node(val=15), right=Node(val=7)))
depth = max_depth(root)
write (*,*) "Example 1:", depth
! Example 2
root => Node(val=1, left=Node(val=2))
depth = max_depth(root)
write (*,*) "Example 2:", depth
end program main
temp.f95:13:23: 13 | type(Node), pointer, intent(in) :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:20:23: 12 | function max_depth(root) result(depth) | 2 ...... 20 | function max_depth(root) result(depth) | 1 Error: Procedure ‘max_depth’ at (1) is already defined at (2) temp.f95:21:47: 21 | type(Node), pointer, intent(in) :: root | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:22:24: 22 | integer :: depth | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:34: 23 | if (associated(root)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:40: 24 | depth = max(max_depth(root%left), max_depth(root%right)) + 1 | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:25:12: 25 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:26:21: 26 | depth = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:11: 27 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:7: 28 | end function max_depth | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:9: 33 | use BinaryTree | 1 Fatal Error: Cannot open module file ‘binarytree.mod’ for reading at (1): No such file or directory compilation terminated.
def maxDepth(root):
if root is None:
return 0
return 1 + max(maxDepth(root.left), maxDepth(root.right))
The algorithm is a simple recursive traversal of the binary tree. We start from the root, and for each node, we calculate the maximum depth of its left and right subtrees. We return the maximum of these two depths plus 1 (for the current node). If the node is null, we return a depth of 0 since this path doesn't exist. This way, we traverse all potential paths in the tree and return the longest one.
int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
The algorithm is a simple recursive traversal of the binary tree. We start from the root, and for each node, we calculate the maximum depth of its left and right subtrees. We return the maximum of these two depths plus 1 (for the current node). If the node is null, we return a depth of 0 since this path doesn't exist. This way, we traverse all potential paths in the tree and return the longest one.