You are given the root
of a full binary tree with the following properties:
0
or 1
, where 0
represents False
and 1
represents True
.2
or 3
, where 2
represents the boolean OR
and 3
represents the boolean AND
.The evaluation of a node is as follows:
True
or False
.Return the boolean result of evaluating the root
node.
A full binary tree is a binary tree where each node has either 0
or 2
children.
A leaf node is a node that has zero children.
Example 1:
Input: root = [2,1,3,null,null,0,1] Output: true Explanation: The above diagram illustrates the evaluation process. The AND node evaluates to False AND True = False. The OR node evaluates to True OR False = True. The root node evaluates to True, so we return true.
Example 2:
Input: root = [0] Output: false Explanation: The root node is a leaf node and it evaluates to false, so we return false.
Constraints:
[1, 1000]
.0 <= Node.val <= 3
0
or 2
children.0
or 1
.2
or 3
.program main
use, intrinsic :: iso_fortran_env, only : error_unit, output_unit
implicit none
integer, parameter :: n = 7
integer, dimension(n) :: values = [2, 1, 3, 0, 0, 0, 1]
type(binary_tree), pointer :: root
logical :: result
call build_tree(values, root)
result = evaluate_tree(root)
write (output_unit, '(*(g0))') 'Result: ', result
contains
subroutine build_tree(values, root)
implicit none
integer, dimension(:), intent(in) :: values
type(binary_tree), pointer, intent(out) :: root
integer :: i, j, k
allocate(root)
if (size(values) == 1) then
root%value = values(1)
else
root%value = values(1)
do i = 2, size(values) - 1, 2
allocate(root%left)
call build_tree(values(i:i+1), root%left)
end do
do i = 3, size(values) - 1, 2
allocate(root%right)
call build_tree(values(i:i+1), root%right)
end do
end if
end subroutine build_tree
function evaluate_tree(root) result(result)
implicit none
type(binary_tree), pointer, intent(in) :: root
logical :: result
if (.not. associated(root)) then
result = .false.
else if (associated(root%left, root%right)) then
result = evaluate_tree(root%left) .and. evaluate_tree(root%right)
else if (associated(root%left)) then
result = evaluate_tree(root%left) .or. root%value == 1
else if (associated(root%right)) then
result = evaluate_tree(root%right) .or. root%value == 1
else
result = root%value == 1
end if
end function evaluate_tree
end program main
type :: binary_tree
integer :: value
class(binary_tree), pointer :: left => null(), right => null()
end type binary_tree
temp.f95:6:22: 6 | type(binary_tree), pointer :: root | 1 Error: Derived type ‘binary_tree’ at (1) is being used before it is defined temp.f95:18:26: 18 | type(binary_tree), pointer, intent(out) :: root | 1 Error: Derived type ‘binary_tree’ at (1) is being used before it is defined temp.f95:21:17: 21 | allocate(root) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:23:18: 23 | root%value = values(1) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:25:18: 25 | root%value = values(1) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:27:31: 27 | allocate(root%left) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:28:53: 28 | call build_tree(values(i:i+1), root%left) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:31:31: 31 | allocate(root%right) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:32:53: 32 | call build_tree(values(i:i+1), root%right) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:39:26: 39 | type(binary_tree), pointer, intent(in) :: root | 1 Error: Derived type ‘binary_tree’ at (1) is being used before it is defined temp.f95:44:34: 44 | else if (associated(root%left, root%right)) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:45:41: 45 | result = evaluate_tree(root%left) .and. evaluate_tree(root%right) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:46:34: 46 | else if (associated(root%left)) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:47:41: 47 | result = evaluate_tree(root%left) .or. root%value == 1 | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:48:34: 48 | else if (associated(root%right)) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:49:41: 49 | result = evaluate_tree(root%right) .or. root%value == 1 | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:51:27: 51 | result = root%value == 1 | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:1:12: 1 | program main | 1 ...... 57 | type :: binary_tree | 2 Error: Two main PROGRAMs at (1) and (2)
module BinaryTreeEvaluation
implicit none
private
public :: evaluate_binary_tree
contains
logical function evaluate_binary_tree(root) result(result)
type(binary_tree_node), pointer, intent(in) :: root
logical :: left_result, right_result
if (.not. associated(root)) then
result = .false.
return
end if
if (root%left_child%is_leaf) then
left_result = root%left_child%value == 1
else
left_result = evaluate_binary_tree(root%left_child)
end if
if (root%right_child%is_leaf) then
right_result = root%right_child%value == 1
else
right_result = evaluate_binary_tree(root%right_child)
end if
select case (root%value)
case (2)
result = left_result .or. right_result
case (3)
result = left_result .and. right_result
case default
result = .false.
end select
end function evaluate_binary_tree
type binary_tree_node
integer :: value = 0
logical :: is_leaf = .false.
type(binary_tree_node), pointer :: left_child => null()
type(binary_tree_node), pointer :: right_child => null()
end type binary_tree_node
end module BinaryTreeEvaluation
program test_binary_tree_evaluation
use BinaryTreeEvaluation
implicit none
type(binary_tree_node), target :: root
logical :: result
! Example 1
root%value = 2
root%left_child%value = 1
root%right_child%value = 3
root%left_child%left_child%value = 0
root%left_child%right_child%value = 1
result = evaluate_binary_tree(root)
write (*,*) "Example 1: ", result
! Example 2
root%value = 0
result = evaluate_binary_tree(root)
write (*,*) "Example 2: ", result
end program test_binary_tree_evaluation
temp.f95:7:31: 7 | type(binary_tree_node), pointer, intent(in) :: root | 1 Error: Derived type ‘binary_tree_node’ at (1) is being used before it is defined temp.f95:15:18: 15 | if (root%left_child%is_leaf) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:16:32: 16 | left_result = root%left_child%value == 1 | 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:18:53: 18 | left_result = evaluate_binary_tree(root%left_child) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:19:11: 19 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:21:18: 21 | if (root%right_child%is_leaf) then | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:22:33: 22 | right_result = root%right_child%value == 1 | 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:24:54: 24 | right_result = evaluate_binary_tree(root%right_child) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:25:11: 25 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:27:27: 27 | select case (root%value) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:28:17: 28 | case (2) | 1 Error: Unexpected CASE statement at (1) temp.f95:30:17: 30 | case (3) | 1 Error: Unexpected CASE statement at (1) temp.f95:32:17: 32 | case default | 1 Error: Unexpected CASE statement at (1) temp.f95:34:11: 34 | end select | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:37:25: 37 | type binary_tree_node | 1 Error: Unexpected derived type declaration statement in CONTAINS section at (1) temp.f95:38:28: 38 | integer :: value = 0 | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:39:36: 39 | logical :: is_leaf = .false. | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:40:31: 40 | type(binary_tree_node), pointer :: left_child => null() | 1 Error: Derived type ‘binary_tree_node’ at (1) is being used before it is defined temp.f95:41:31: 41 | type(binary_tree_node), pointer :: right_child => null() | 1 Error: Derived type ‘binary_tree_node’ at (1) is being used before it is defined temp.f95:42:7: 42 | end type binary_tree_node | 1 Error: Expecting END MODULE statement at (1) temp.f95:6:46: 6 | logical function evaluate_binary_tree(root) result(result) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:46:9: 46 | use BinaryTreeEvaluation | 1 Fatal Error: Cannot open module file ‘binarytreeevaluation.mod’ for reading at (1): No such file or directory compilation terminated.
def evalTree(root: 'TreeNode') -> bool:
if not root.left and not root.right:
return root.val == 1
left = evalTree(root.left)
right = evalTree(root.right)
if root.val == 2:
return left or right
return left and right
This problem uses a binary tree to calculate boolean values.
The algorithm for this problem is a recursive depth-first search.
1. If the current node is a leaf node, return its value (True
/ False
).
2. If the current node is a non-leaf node, recursively compute the values of its left and right children.
3. If the current node is an OR
node, return the result of left || right
. If it's an AND
node, return the result of left && right
.
The base case here is when the node is a leaf node, and the recursion will stop, returning its boolean value. At each level of recursion, it will evaluate the operator associated with the node and build up the final result.
bool evalTree(TreeNode* root) {
if (!root->left && !root->right)
return root->val;
bool left = evalTree(root->left);
bool right = evalTree(root->right);
if (root->val == 2)
return left || right;
return left && right;
}
This problem uses a binary tree to calculate boolean values.
The algorithm for this problem is a recursive depth-first search.
1. If the current node is a leaf node, return its value (True
/ False
).
2. If the current node is a non-leaf node, recursively compute the values of its left and right children.
3. If the current node is an OR
node, return the result of left || right
. If it's an AND
node, return the result of left && right
.
The base case here is when the node is a leaf node, and the recursion will stop, returning its boolean value. At each level of recursion, it will evaluate the operator associated with the node and build up the final result.