Given the root
of a binary tree, return the preorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3] Output: [1,2,3]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [1] Output: [1]
Constraints:
[0, 100]
.-100 <= Node.val <= 100
Follow up: Recursive solution is trivial, could you do it iteratively?
program main
use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
implicit none
integer, parameter :: unit_in = 10, unit_out = 20
character(len=*), parameter :: filename_in = "example_input.txt"
character(len=*), parameter :: filename_out = "example_output.txt"
type :: node
real(kind=DP) :: val
type(node), pointer :: left => null(), right => null()
end type
type(node), pointer :: root
integer :: iostat
logical :: exists
! Read input from file
open(newunit=unit_in, file=filename_in, status="old", action="read", &
form="formatted", access="sequential", iostat=iostat, &
position="rewind")
if (iostat /= 0) then
write(unit=error_unit, fmt="(A)") "Error: cannot open input file."
stop 1
end if
exists = .true.
call read_tree(unit_in, root, exists)
close(unit=unit_in)
! Output preorder traversal
write(unit=unit_out, fmt="(*(G0))") preorder_traversal(root)
! Write output to file
open(newunit=unit_out, file=filename_out, status="replace", action="write", &
form="formatted", access="sequential", iostat=iostat, &
position="rewind")
if (iostat /= 0) then
write(unit=error_unit, fmt="(A)") "Error: cannot open output file."
stop 1
end if
call write_preorder_traversal(unit_out, root)
close(unit=unit_out)
contains
function preorder_traversal(root) result(values)
implicit none
class(node), intent(in) :: root
real(kind=DP), allocatable :: values(:)
allocate(values(size(root)))
values = preorder_traversal_recursive(root)
end function
recursive function preorder_traversal_recursive(root) result(values)
implicit none
class(node), intent(in) :: root
real(kind=DP), allocatable :: values(:)
if (.not. associated(root)) then
allocate(values(0))
return
end if
allocate(values(size(root)+1))
values(1) = root%val
values(2:) = preorder_traversal_recursive(root%left)
values(size(values)-1:) = preorder_traversal_recursive(root%right)
end function
subroutine write_preorder_traversal(unit, root)
implicit none
integer, value :: unit
class(node), intent(in) :: root
if (.not. associated(root)) then
return
end if
write(unit=unit, fmt="(G0)") root%val
call write_preorder_traversal(unit, root%left)
call write_preorder_traversal(unit, root%right)
end subroutine
subroutine read_tree(unit, root, exists)
implicit none
integer, value :: unit
class(node), pointer :: root
logical, intent(out) :: exists
real(kind=DP) :: val
exists = .false.
read(unit=unit, fmt=*, iostat=iostat) val
if (iostat == 0) then
exists = .true.
allocate(root)
root%val = val
call read_tree(unit, root%left, exists)
call read_tree(unit, root%right, exists)
end if
end subroutine
end program
temp.f95:89:51: 89 | call read_tree(unit, root%left, exists) | 1 Error: SUBROUTINE ‘read_tree’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:89:33: 89 | call read_tree(unit, root%left, exists) | 1 Error: Actual argument to ‘root’ at (1) must be polymorphic temp.f95:90:52: 90 | call read_tree(unit, root%right, exists) | 1 Error: SUBROUTINE ‘read_tree’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:90:33: 90 | call read_tree(unit, root%right, exists) | 1 Error: Actual argument to ‘root’ at (1) must be polymorphic temp.f95:69:29: 69 | if (.not. associated(root)) then | 1 Error: ‘pointer’ argument of ‘associated’ intrinsic at (1) must be a POINTER temp.f95:73:54: 73 | call write_preorder_traversal(unit, root%left) | 1 Error: SUBROUTINE ‘write_preorder_traversal’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:74:55: 74 | call write_preorder_traversal(unit, root%right) | 1 Error: SUBROUTINE ‘write_preorder_traversal’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:55:29: 55 | if (.not. associated(root)) then | 1 Error: ‘pointer’ argument of ‘associated’ intrinsic at (1) must be a POINTER temp.f95:59:29: 59 | allocate(values(size(root)+1)) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:47:29: 47 | allocate(values(size(root))) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:16:17: 16 | open(newunit=unit_in, file=filename_in, status="old", action="read", & | 1 Error: Named constant ‘unit_in’ in variable definition context (NEWUNIT tag) at (1) temp.f95:24:28: 24 | call read_tree(unit_in, root, exists) | 1 Error: Actual argument to ‘root’ at (1) must be polymorphic temp.f95:31:17: 31 | open(newunit=unit_out, file=filename_out, status="replace", action="write", & | 1 Error: Named constant ‘unit_out’ in variable definition context (NEWUNIT tag) at (1)
module binary_tree_preorder_traversal
implicit none
private
public :: binary_tree_preorder_traversal
type :: binary_tree_node
integer :: val
type(binary_tree_node), pointer :: left
type(binary_tree_node), pointer :: right
end type binary_tree_node
contains
function binary_tree_preorder_traversal(root) result(preorder)
type(binary_tree_node), pointer, intent(in) :: root
integer, allocatable :: preorder(:)
integer :: i, n
! Count the number of nodes in the tree
n = count_nodes(root)
! Allocate an array to store the preorder traversal
allocate(preorder(n))
! Perform the preorder traversal
i = 1
call preorder_traversal(root, preorder, i)
end function binary_tree_preorder_traversal
recursive subroutine preorder_traversal(root, preorder, i)
type(binary_tree_node), pointer, intent(in) :: root
integer, allocatable, intent(inout) :: preorder(:)
integer, intent(inout) :: i
! Base case: if the root is null, return
if (.not. associated(root)) return
! Store the root's value in the preorder array
preorder(i) = root%val
i = i + 1
! Recursively traverse the left subtree
call preorder_traversal(root%left, preorder, i)
! Recursively traverse the right subtree
call preorder_traversal(root%right, preorder, i)
end subroutine preorder_traversal
recursive function count_nodes(root) result(n)
type(binary_tree_node), pointer, intent(in) :: root
integer :: n
! Base case: if the root is null, return 0
if (.not. associated(root)) then
n = 0
return
end if
! Recursively count the number of nodes in the left and right subtrees
n = 1 + count_nodes(root%left) + count_nodes(root%right)
end function count_nodes
end module binary_tree_preorder_traversal
program test
use binary_tree_preorder_traversal
implicit none
! Test case 1: Empty tree
type(binary_tree_node), pointer :: root
integer, allocatable :: preorder(:)
root => null()
preorder = binary_tree_preorder_traversal(root)
write(*,*) preorder
! Test case 2: Single node
root => binary_tree_node(1, null(), null())
preorder = binary_tree_preorder_traversal(root)
write(*,*) preorder
! Test case 3: Binary tree
root => binary_tree_node(1, &
binary_tree_node(2, null(), null()), &
binary_tree_node(3, null(), null()))
preorder = binary_tree_preorder_traversal(root)
write(*,*) preorder
end program test
temp.f95:4:40: 4 | public :: binary_tree_preorder_traversal | 1 Error: PUBLIC attribute applied to MODULE binary_tree_preorder_traversal at (1) temp.f95:14:39: 14 | function binary_tree_preorder_traversal(root) result(preorder) | 1 Error: MODULE attribute of ‘binary_tree_preorder_traversal’ conflicts with PROCEDURE attribute at (1) temp.f95:15:55: 15 | type(binary_tree_node), pointer, intent(in) :: root | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:16:39: 16 | integer, allocatable :: preorder(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:19: 17 | integer :: i, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:25: 20 | n = count_nodes(root) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:13: 23 | allocate(preorder(n)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:26:9: 26 | i = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:46: 27 | call preorder_traversal(root, preorder, i) | 1 Error: Unexpected CALL statement in CONTAINS section at (1) temp.f95:29:3: 29 | end function binary_tree_preorder_traversal | 1 Error: Expecting END MODULE statement at (1) temp.f95:69:9: 69 | use binary_tree_preorder_traversal | 1 Fatal Error: Cannot open module file ‘binary_tree_preorder_traversal.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 preorderTraversal(root):
if root is None:
return []
return [root.val] + preorderTraversal(root.left) + preorderTraversal(root.right)
The algorithm performs a preorder traversal of the binary tree, which involves visiting the root node, then the left subtree, and finally the right subtree. This is a simple recursive algorithm:
null
), return an empty list.For a non-empty binary tree with root r
, left subtree left
, and right subtree right
, return a list containing:
r.val
preorderTraversal(left)
.preorderTraversal(right)
.Note that this algorithm can be implemented in each language using similar concepts, but the syntax for concatenating lists in each language may differ.
#include<vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
std::vector<int> preorderTraversal(TreeNode* root) {
std::vector<int> result;
if (root == nullptr) {
return result;
}
result.push_back(root->val);
std::vector<int> left = preorderTraversal(root->left);
std::vector<int> right = preorderTraversal(root->right);
result.insert(result.end(), left.begin(), left.end());
result.insert(result.end(), right.begin(), right.end());
return result;
}
The algorithm performs a preorder traversal of the binary tree, which involves visiting the root node, then the left subtree, and finally the right subtree. This is a simple recursive algorithm:
null
), return an empty list.For a non-empty binary tree with root r
, left subtree left
, and right subtree right
, return a list containing:
r.val
preorderTraversal(left)
.preorderTraversal(right)
.Note that this algorithm can be implemented in each language using similar concepts, but the syntax for concatenating lists in each language may differ.