Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
Example 1:
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9] Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
Example 2:
Input: root = [5,1,7] Output: [1,null,5,null,7]
Constraints:
[1, 100].0 <= Node.val <= 1000program main
use, intrinsic :: iso_fortran_env, only : error_unit, iostat_end
implicit none
integer, parameter :: unit_in = 10, unit_out = 20
character(len=256) :: filename
type(error_type), allocatable :: err
type(tree_node), pointer :: root
! Read input from file
write (unit=unit_out, fmt='(A)') 'Enter input filename:'
read (unit=unit_in, fmt='(A)', iostat=err%stat, iomsg=err%msg, &
iolength=err%n) filename
if (err%stat /= 0) then
write (unit=error_unit, fmt='(2A)') 'error: ', trim(err%msg)
stop 1
end if
open (newunit=unit_in, file=filename, status='old', &
action='read', access='stream', form='unformatted', &
iostat=err%stat, iomsg=err%msg)
if (err%stat /= 0) then
write (unit=error_unit, fmt='(2A)') 'error: ', trim(err%msg)
stop 1
end if
call read_tree(unit_in, root)
close (unit=unit_in)
! Rearrange tree
call rearrange_tree(root)
! Write output to stdout
call print_tree(root, unit_out)
contains
subroutine read_tree(unit, root)
implicit none
integer, intent(in) :: unit
type(tree_node), pointer, intent(out) :: root
class(tree_node), pointer :: current
integer :: stat
allocate (current)
current => root
do
read (unit=unit, fmt=*, iostat=stat) current%val
if (stat == iostat_end) exit
allocate (current%right)
current => current%right
end do
nullify (current%right)
end subroutine read_tree
subroutine rearrange_tree(root)
implicit none
type(tree_node), pointer, intent(inout) :: root
type(tree_node), pointer :: current, next
current => root
do while (associated(current))
if (associated(current%left)) then
next => current%left
current%left => null()
current => next
else
next => current%right
current%right => null()
current => next
end if
end do
end subroutine rearrange_tree
subroutine print_tree(root, unit)
implicit none
type(tree_node), pointer, intent(in) :: root
integer, intent(in) :: unit
class(tree_node), pointer :: current
current => root
do while (associated(current))
write (unit=unit, fmt='(I0)') current%val
if (associated(current%left)) then
current => current%left
else if (associated(current%right)) then
current => current%right
else
exit
end if
end do
end subroutine print_tree
end program main
type :: tree_node
integer :: val
type(tree_node), pointer :: left => null(), right => null()
end type tree_node
temp.f95:6:21:
6 | type(error_type), allocatable :: err
| 1
Error: Derived type āerror_typeā at (1) is being used before it is defined
temp.f95:7:20:
7 | type(tree_node), pointer :: root
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:11:43:
11 | read (unit=unit_in, fmt='(A)', iostat=err%stat, iomsg=err%msg, &
| 1
Error: Invalid value for IOSTAT specification at (1)
temp.f95:13:13:
13 | if (err%stat /= 0) then
| 1
Error: Symbol āerrā at (1) has no IMPLICIT type
temp.f95:14:65:
14 | write (unit=error_unit, fmt='(2A)') 'error: ', trim(err%msg)
| 1
Error: Symbol āerrā at (1) has no IMPLICIT type
temp.f95:16:7:
16 | end if
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:19:18:
19 | iostat=err%stat, iomsg=err%msg)
| 1
Error: Invalid value for IOSTAT specification at (1)
temp.f95:20:13:
20 | if (err%stat /= 0) then
| 1
Error: Symbol āerrā at (1) has no IMPLICIT type
temp.f95:21:65:
21 | write (unit=error_unit, fmt='(2A)') 'error: ', trim(err%msg)
| 1
Error: Symbol āerrā at (1) has no IMPLICIT type
temp.f95:23:7:
23 | end if
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:38:24:
38 | type(tree_node), pointer, intent(out) :: root
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:39:25:
39 | class(tree_node), pointer :: current
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:42:18:
42 | allocate (current)
| 1
Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable
temp.f95:45:58:
45 | read (unit=unit, fmt=*, iostat=stat) current%val
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:47:31:
47 | allocate (current%right)
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:48:32:
48 | current => current%right
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:50:26:
50 | nullify (current%right)
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:55:24:
55 | type(tree_node), pointer, intent(inout) :: root
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:56:24:
56 | type(tree_node), pointer :: current, next
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:60:36:
60 | if (associated(current%left)) then
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:61:33:
61 | next => current%left
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:62:25:
62 | current%left => null()
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:64:16:
64 | else
| 1
Error: Unexpected ELSE statement at (1)
temp.f95:65:33:
65 | next => current%right
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:66:25:
66 | current%right => null()
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:68:15:
68 | end if
| 1
Error: Expecting END DO statement at (1)
temp.f95:74:24:
74 | type(tree_node), pointer, intent(in) :: root
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:76:25:
76 | class(tree_node), pointer :: current
| 1
Error: Derived type ātree_nodeā at (1) is being used before it is defined
temp.f95:80:51:
80 | write (unit=unit, fmt='(I0)') current%val
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:81:36:
81 | if (associated(current%left)) then
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:82:36:
82 | current => current%left
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:83:41:
83 | else if (associated(current%right)) then
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:84:36:
84 | current => current%right
| 1
Error: Symbol ācurrentā at (1) has no IMPLICIT type
temp.f95:85:16:
85 | else
| 1
Error: Unexpected ELSE statement at (1)
temp.f95:87:15:
87 | end if
| 1
Error: Expecting END DO statement at (1)
temp.f95:1:12:
1 | program main
| 1
......
93 | type :: tree_node
| 2
Error: Two main PROGRAMs at (1) and (2)
module BST
! Declare the node structure
type :: Node
integer :: val
type(Node), pointer :: left
type(Node), pointer :: right
end type
contains
! Function to rearrange the tree
recursive function rearrange(root) result(new_root)
type(Node), pointer :: root
type(Node), pointer :: new_root
type(Node), pointer :: current
type(Node), pointer :: previous
! Base case: if the root is null, return null
if (.not. associated(root)) then
nullify(new_root)
return
end if
! Recursive case: rearrange the left subtree
current => root
previous => null()
call rearrange(current%left, new_root)
! Connect the previous node to the current node
if (associated(previous)) then
previous%right => current
end if
! Connect the current node to the right subtree
current%left => null()
call rearrange(current%right, current%right)
! Return the new root
new_root => current
end function
end module
program main
use BST
implicit none
! Test case 1
type(Node), pointer :: root
type(Node), pointer :: new_root
integer, dimension(10) :: values = [5, 3, 6, 2, 4, 0, 8, 1, 0, 7, 9]
! Create the tree
root => null()
do i = 1, size(values)
call insert(root, values(i))
end do
! Rearrange the tree
new_root => rearrange(root)
! Print the tree
call print_tree(new_root)
! Test case 2
root => null()
call insert(root, 5)
call insert(root, 1)
call insert(root, 7)
! Rearrange the tree
new_root => rearrange(root)
! Print the tree
call print_tree(new_root)
contains
! Function to insert a value in the tree
recursive subroutine insert(root, val)
type(Node), pointer :: root
integer :: val
type(Node), pointer :: current
! Base case: if the root is null, create a new node
if (.not. associated(root)) then
allocate(root)
root%val = val
root%left => null()
root%right => null()
return
end if
! Recursive case: insert the value in the left or right subtree
current => root
if (val < current%val) then
call insert(current%left, val)
else
call insert(current%right, val)
end if
end subroutine
! Function to print the tree
recursive subroutine print_tree(root)
type(Node), pointer :: root
type(Node), pointer :: current
! Base case: if the root is null, return
if (.not. associated(root)) then
return
end if
! Print the left subtree
current => root
call print_tree(current%left)
! Print the current node
write (*,*) current%val
! Print the right subtree
call print_tree(current%right)
end subroutine
end program
temp.f95:13:0:
13 | recursive function rearrange(root) result(new_root)
|
......
28 | call rearrange(current%left, new_root)
| 2
Error: ārearrangeā at (1) has a type, which is not consistent with the CALL at (2)
temp.f95:13:0:
13 | recursive function rearrange(root) result(new_root)
|
......
37 | call rearrange(current%right, current%right)
| 2
Error: ārearrangeā at (1) has a type, which is not consistent with the CALL at (2)
temp.f95:47:9:
47 | use BST
| 1
Fatal Error: Cannot open module file ābst.modā for reading at (1): No such file or directory
compilation terminated.
def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
def is_palindrome(n):
return str(n) == str(n)[::-1]
def prime_palindrome(n):
while True:
if is_prime(n) and is_palindrome(n):
return n
n += 1
The algorithm consists of three functions: isPrime, isPalindrome, and primePalindrome. The first function, isPrime, checks if a given number is prime by iterating from 2 to the square root of the number and checking if the number has any divisors. If not, the function returns true, indicating prime.
The second function, isPalindrome, checks if a given number is a palindrome by converting the number to a string and then reversing the string. If the original and reversed strings are the same, the function returns true, indicating a palindrome.
The main function, primePalindrome, takes an integer n as input and iterates from n onward, checking if each number is both prime and a palindrome using the isPrime and isPalindrome helper functions. When a number is found to be both prime and a palindrome, it is returned as the answer.
The algorithm has a complexity of O(sqrt(n) * n) in the worst case because isPrime has a complexity of O(sqrt(n)) and both isPalindrome and primePalindrome have complexities of O(n).
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
bool isPalindrome(int n) {
int rev = 0, orig = n;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return orig == rev;
}
int primePalindrome(int n) {
while (true) {
if (isPrime(n) && isPalindrome(n)) return n;
n++;
}
}
The algorithm consists of three functions: isPrime, isPalindrome, and primePalindrome. The first function, isPrime, checks if a given number is prime by iterating from 2 to the square root of the number and checking if the number has any divisors. If not, the function returns true, indicating prime.
The second function, isPalindrome, checks if a given number is a palindrome by converting the number to a string and then reversing the string. If the original and reversed strings are the same, the function returns true, indicating a palindrome.
The main function, primePalindrome, takes an integer n as input and iterates from n onward, checking if each number is both prime and a palindrome using the isPrime and isPalindrome helper functions. When a number is found to be both prime and a palindrome, it is returned as the answer.
The algorithm has a complexity of O(sqrt(n) * n) in the worst case because isPrime has a complexity of O(sqrt(n)) and both isPalindrome and primePalindrome have complexities of O(n).