You are given the root
of a binary search tree (BST) and an integer val
.
Find the node in the BST that the node's value equals val
and return the subtree rooted with that node. If such a node does not exist, return null
.
Example 1:
Input: root = [4,2,7,1,3], val = 2 Output: [2,1,3]
Example 2:
Input: root = [4,2,7,1,3], val = 5 Output: []
Constraints:
[1, 5000]
.1 <= Node.val <= 107
root
is a binary search tree.1 <= val <= 107
program main
use, intrinsic :: iso_fortran_env, only : error_unit, iostat_end
implicit none
type(node), pointer :: root
integer :: val
integer :: stat
call get_arguments(root, val)
if (associated(root)) then
write (unit=error_unit, fmt='(*(g0))') 'Root: ', root%value
write (unit=error_unit, fmt='(*(g0))') 'Val: ', val
write (unit=error_unit, fmt='(*(g0))') 'Result: ', find_node(root, val)
else
write (unit=error_unit, fmt='(*(g0))') 'No result found.'
end if
contains
function find_node(root, val) result(result)
class(node), pointer, intent(in) :: root
integer, intent(in) :: val
class(node), pointer :: result
if (.not. associated(root)) then
nullify(result)
return
end if
if (root%value == val) then
result => root
else if (val < root%value) then
result => find_node(root%left, val)
else
result => find_node(root%right, val)
end if
end function find_node
subroutine get_arguments(root, val)
type(node), pointer :: root
integer, intent(out) :: val
character(len=:), allocatable :: line
integer :: ios
read (unit=input_unit, fmt='(A)', iostat=ios) line
if (ios /= 0) then
if (ios /= iostat_end) then
write (unit=error_unit, fmt='(*(g0))') 'Error reading input.'
stop 1
end if
return
end if
allocate (character(len=len(line)) :: line)
line = adjustl(line)
read (unit=line, fmt=*, iostat=ios) root, val
if (ios /= 0) then
write (unit=error_unit, fmt='(*(g0))') 'Error parsing input.'
stop 1
end if
end subroutine get_arguments
type, public :: node
integer :: value
class(node), pointer :: left => null(), right => null()
end type node
end program main
temp.f95:4:15: 4 | type(node), pointer :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:11:63: 11 | write (unit=error_unit, fmt='(*(g0))') 'Root: ', root%value | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:21:20: 21 | class(node), pointer, intent(in) :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:23:20: 23 | class(node), pointer :: result | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:20:48: 20 | function find_node(root, val) result(result) | 1 Error: Function result ‘result’ at (1) has no IMPLICIT type temp.f95:30:18: 30 | if (root%value == val) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:32:29: 32 | else if (val < root%value) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:33:38: 33 | result => find_node(root%left, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:34:12: 34 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:35:38: 35 | result => find_node(root%right, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:36:11: 36 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:40:19: 40 | type(node), pointer :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:63:17: 63 | type, public :: node | 1 Error: Derived type at (1) can only be PUBLIC in the specification part of a module temp.f95:64:24: 64 | integer :: value | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:65:20: 65 | class(node), pointer :: left => null(), right => null() | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:66:7: 66 | end type node | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:20:27: 20 | function find_node(root, val) result(result) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:39:33: 39 | subroutine get_arguments(root, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:8:27: 8 | call get_arguments(root, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:45:29: 45 | read (unit=input_unit, fmt='(A)', iostat=ios) line | 1 Error: Symbol ‘input_unit’ at (1) has no IMPLICIT type
module bst_search
implicit none
type :: bst_node
integer :: val
type(bst_node), pointer :: left
type(bst_node), pointer :: right
end type bst_node
contains
type(bst_node), pointer function search(root, val) result(subtree)
type(bst_node), pointer :: root
integer :: val
if (associated(root)) then
if (root%val == val) then
subtree => root
else if (val < root%val) then
subtree => search(root%left, val)
else
subtree => search(root%right, val)
end if
end if
end function search
end module bst_search
program test_bst_search
use bst_search
implicit none
type(bst_node), pointer :: root
integer :: val
! Example 1
root => bst_node(val=4, left=>bst_node(val=2, left=>bst_node(val=1), right=>bst_node(val=3)), right=>bst_node(val=7))
val = 2
write (*,*) 'Example 1: '
call print_subtree(search(root, val))
! Example 2
root => bst_node(val=4, left=>bst_node(val=2, left=>bst_node(val=1), right=>bst_node(val=3)), right=>bst_node(val=7))
val = 5
write (*,*) 'Example 2: '
call print_subtree(search(root, val))
contains
subroutine print_subtree(root)
type(bst_node), pointer :: root
if (associated(root)) then
write (*,*) root%val
call print_subtree(root%left)
call print_subtree(root%right)
end if
end subroutine print_subtree
end program test_bst_search
temp.f95:13:16: 13 | type(bst_node), pointer function search(root, val) result(subtree) | 1 Error: Invalid character in name at (1) temp.f95:14:35: 14 | type(bst_node), pointer :: root | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:18: 15 | integer :: val | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:30: 17 | if (associated(root)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:18: 18 | if (root%val == val) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:19:27: 19 | subtree => root | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:20:29: 20 | else if (val < root%val) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:21:36: 21 | subtree => search(root%left, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:22:12: 22 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:23:36: 23 | subtree => search(root%right, val) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:24:11: 24 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:7: 25 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:3: 26 | end function search | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:9: 31 | use bst_search | 1 Fatal Error: Cannot open module file ‘bst_search.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 searchBST(root: TreeNode, val: int) -> TreeNode:
if not root or root.val == val:
return root
return searchBST(root.right, val) if root.val < val else searchBST(root.left, val)
We use a recursive search function for searching the BST. The basic logic is as follows:
null
or its value equals the target value. If so, return the node itself.In all four programming languages, the implementation is pretty much the same:
searchBST
function.struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
};
TreeNode* searchBST(TreeNode* root, int val) {
if (!root || root->val == val)
return root;
return (root->val < val) ? searchBST(root->right, val) : searchBST(root->left, val);
}
We use a recursive search function for searching the BST. The basic logic is as follows:
1. Check if the current node is null
or its value equals the target value. If so, return the node itself.
2. If the target value is greater than the current node's value, search the right child.
3. If the target value is less than the current node's value, search the left child.
In all four programming languages, the implementation is pretty much the same:
searchBST
function.