Given the root
of a binary search tree and an integer k
, return true
if there exist two elements in the BST such that their sum is equal to k
, or false
otherwise.
Example 1:
Input: root = [5,3,6,2,4,null,7], k = 9 Output: true
Example 2:
Input: root = [5,3,6,2,4,null,7], k = 28 Output: false
Constraints:
[1, 104]
.-104 <= Node.val <= 104
root
is guaranteed to be a valid binary search tree.-105 <= k <= 105
program main
use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
implicit none
integer, parameter :: n = 10
type(DP), dimension(n) :: x, y
logical :: res
! Example 1
x = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /)
y = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /)
call solve(x, y, 9._DP, res)
write (unit=error_unit, fmt='(L1)') res
! Example 2
x = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /)
y = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /)
call solve(x, y, 28._DP, res)
write (unit=error_unit, fmt='(L1)') res
contains
subroutine solve(root, k, res)
implicit none
type(DP), dimension(:), intent(in) :: root
real(kind=DP), intent(in) :: k
logical, intent(out) :: res
integer :: i, j
res = .false.
do i = 1, size(root)-1
do j = i+1, size(root)
if (root(i)+root(j) == k) then
res = .true.
exit
end if
end do
if (res) exit
end do
end subroutine solve
end program main
temp.f95:5:13: 2 | use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64 | 2 ...... 5 | type(DP), dimension(n) :: x, y | 1 Error: Type name ‘dp’ at (1) conflicts with previously declared entity at (2), which has the same name temp.f95:24:17: 2 | use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64 | 2 ...... 24 | type(DP), dimension(:), intent(in) :: root | 1 Error: Type name ‘dp’ at (1) conflicts with previously declared entity at (2), which has the same name temp.f95:22:25: 22 | subroutine solve(root, k, res) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:9:5: 9 | x = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /) | 1 Error: Symbol ‘x’ at (1) has no IMPLICIT type temp.f95:10:5: 10 | y = (/ 5._DP, 3._DP, 6._DP, 2._DP, 4._DP, -1._DP, 7._DP /) | 1 Error: Symbol ‘y’ at (1) has no IMPLICIT type temp.f95:32:32: 32 | if (root(i)+root(j) == k) then | 1 Error: Function ‘root’ at (1) has no IMPLICIT type
module BSTSum
implicit none
private
public :: main
contains
logical function sumExists(root, k) result(exists)
type(Node), pointer, intent(in) :: root
integer, intent(in) :: k
logical :: left, right
exists = .false.
if (root%left /= null()) then
left = sumExists(root%left, k)
else
left = .false.
end if
if (root%right /= null()) then
right = sumExists(root%right, k)
else
right = .false.
end if
if (left .or. right) then
exists = .true.
else
if (root%val == k) then
exists = .true.
end if
end if
end function sumExists
subroutine main
type(Node), pointer :: root
integer :: k
logical :: exists
! Read the root node and the value k
read *, root
read *, k
! Call the sumExists function and print the result
exists = sumExists(root, k)
write (*, '(A)', advance='no') merge('True', 'False', exists)
end subroutine main
type :: Node
integer :: val
type(Node), pointer :: left
type(Node), pointer :: right
end type Node
end module BSTSum
temp.f95:9:19: 9 | type(Node), pointer, intent(in) :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:15:18: 15 | if (root%left /= null()) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:16:35: 16 | left = sumExists(root%left, k) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:17:12: 17 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:19:11: 19 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:21:18: 21 | if (root%right /= null()) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:22:36: 22 | right = sumExists(root%right, k) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:23:12: 23 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:25:11: 25 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:30:22: 30 | if (root%val == k) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:33:11: 33 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:37:19: 37 | type(Node), pointer :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:50:16: 50 | type :: Node | 1 Error: Unexpected derived type declaration statement in CONTAINS section at (1) temp.f95:51:22: 51 | integer :: val | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:52:19: 52 | type(Node), pointer :: left | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:53:19: 53 | type(Node), pointer :: right | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:54:7: 54 | end type Node | 1 Error: Expecting END MODULE statement at (1) temp.f95:8:35: 8 | logical function sumExists(root, k) result(exists) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:42:20: 42 | read *, root | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:47:45: 47 | write (*, '(A)', advance='no') merge('True', 'False', exists) | 1 Error: Unequal character lengths (4/5) in MERGE intrinsic at (1)
def findTarget(root, k):
nodes = set()
return findNode(root, k, nodes)
def findNode(root, k, nodes):
if not root:
return False
if k - root.val in nodes:
return True
nodes.add(root.val)
return findNode(root.left, k, nodes) or findNode(root.right, k, nodes)
The function takes the root of a binary search tree and a target number k
. The purpose is to see if there are two elements in the tree that sum up to k
. We start by initializing a set called nodes
to keep track of visited values. Then, we create a recursive helper function called findNode
.
In the findNode
function, the base case is when the current root is null, meaning we've reached the end of a branch, and we return false as no such pair of elements found yet. If k - root.val
is found in our set, that means we've found a pair of elements that sum up to k
, and we can return true. Otherwise, we proceed with the left and right children of the current root, passing the nodes
set along to keep track of visited values. This is done using a depth-first search strategy.
The result is a boolean indicating whether or not two elements in the tree sum up to k
.
bool findTarget(TreeNode* root, int k) {
unordered_set<int> nodes;
return findNode(root, k, nodes);
}
bool findNode(TreeNode* root, int k, unordered_set<int>& nodes) {
if (!root) return false;
if (nodes.count(k - root->val)) return true;
nodes.insert(root->val);
return findNode(root->left, k, nodes) || findNode(root->right, k, nodes);
}
The function takes the root of a binary search tree and a target number k
. The purpose is to see if there are two elements in the tree that sum up to k
. We start by initializing a set called nodes
to keep track of visited values. Then, we create a recursive helper function called findNode
.
In the findNode
function, the base case is when the current root is null, meaning we've reached the end of a branch, and we return false as no such pair of elements found yet. If k - root.val
is found in our set, that means we've found a pair of elements that sum up to k
, and we can return true. Otherwise, we proceed with the left and right children of the current root, passing the nodes
set along to keep track of visited values. This is done using a depth-first search strategy.
The result is a boolean indicating whether or not two elements in the tree sum up to k
.