Given the root
of an n-ary tree, return the preorder traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
Example 1:
Input: root = [1,null,3,2,4,null,5,6] Output: [1,3,5,6,2,4]
Example 2:
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
Constraints:
[0, 104]
.0 <= Node.val <= 104
1000
.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 = "narytree.txt"
character(len=*), parameter :: filename_out = "preordertraversal.txt"
type :: node
real(kind=DP) :: val
class(*), pointer :: left => null(), right => null()
end type
type(node), pointer :: root => null()
integer :: i, j, k, l, m, n
logical :: eof
real(kind=DP) :: x
! Read the input file
open(newunit=unit_in, file=filename_in, status='old', action='read', &
form='formatted', access='sequential', position='rewind', &
iomsg=i, iostat=j)
if (j /= 0) then
write(unit=error_unit, fmt='(A,I0,A)') 'Error: Can not open file ', j, ' for reading.'
stop 1
end if
read(unit=unit_in, fmt=*, iomsg=k, iostat=l) root
close(unit=unit_in)
if (k /= '') then
write(unit=error_unit, fmt='(A,A)') 'Error: Reading from file ', trim(k), ' failed.'
stop 2
end if
if (l /= 0) then
write(unit=error_unit, fmt='(A,I0,A)') 'Error: Reading from file ', l, ' failed.'
stop 3
end if
! Print the preorder traversal of the nodes' values
call print_preorder_traversal(root)
contains
subroutine print_preorder_traversal(root)
implicit none
class(node), pointer, intent(in) :: root
logical :: first
first = .true.
do while (associated(root))
if (.not.first) then
write(unit=unit_out, fmt='(A)', advance='no') ','
else
first = .false.
end if
write(unit=unit_out, fmt='(ES24.16)') root%val
call print_preorder_traversal(root%left)
call print_preorder_traversal(root%right)
end do
end subroutine print_preorder_traversal
end program main
temp.f95:52:52: 52 | call print_preorder_traversal(root%left) | 1 Error: SUBROUTINE ‘print_preorder_traversal’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:52:52: 52 | call print_preorder_traversal(root%left) | 1 Error: Type mismatch in argument ‘root’ at (1); passed CLASS(*) to CLASS(node) temp.f95:53:53: 53 | call print_preorder_traversal(root%right) | 1 Error: SUBROUTINE ‘print_preorder_traversal’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:53:53: 53 | call print_preorder_traversal(root%right) | 1 Error: Type mismatch in argument ‘root’ at (1); passed CLASS(*) to CLASS(node) temp.f95:19:15: 19 | iomsg=i, iostat=j) | 1 Error: IOMSG tag at (1) must be of type CHARACTER temp.f95:24:53: 24 | read(unit=unit_in, fmt=*, iomsg=k, iostat=l) root | 1 Error: Data transfer element at (1) cannot have POINTER components unless it is processed by a defined input/output procedure temp.f95:24:36: 24 | read(unit=unit_in, fmt=*, iomsg=k, iostat=l) root | 1 Error: IOMSG tag at (1) must be of type CHARACTER temp.f95:26:8: 26 | if (k /= '') then | 1 Error: Operands of comparison operator ‘/=’ at (1) are INTEGER(4)/CHARACTER(0) temp.f95:27:78: 27 | write(unit=error_unit, fmt='(A,A)') 'Error: Reading from file ', trim(k), ' failed.' | 1 Error: ‘string’ argument of ‘trim’ intrinsic at (1) must be CHARACTER
module nary_tree_preorder_traversal
implicit none
private
public :: nary_tree_preorder_traversal
contains
function preorder_traversal(root) result(preorder)
! Return the preorder traversal of the n-ary tree root
!
! Args:
! root: The root of the n-ary tree
!
! Returns:
! preorder: The preorder traversal of the n-ary tree
!
! Example:
! root = [1,null,3,2,4,null,5,6]
! preorder = [1,3,5,6,2,4]
!
! root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
! preorder = [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
!
! Constraints:
! The number of nodes in the tree is in the range [0, 104].
! 0 <= Node.val <= 104
! The height of the n-ary tree is less than or equal to 1000.
type(nary_node), pointer, intent(in) :: root
type(nary_node), pointer :: current
integer :: i
integer, allocatable :: preorder(:)
! Allocate memory for the preorder traversal
allocate(preorder(size(root%children)))
! Initialize the current node to the root
current => root
! Iterate through the nodes in the preorder
do i = 1, size(root%children)
! Add the current node's value to the preorder
preorder(i) = current%val
! If the current node has children, add them to the preorder
if (associated(current%children)) then
preorder(i+1:) = preorder(i+1:) // current%children
end if
! Move to the next node
current => current%next
end do
end function preorder_traversal
end module nary_tree_preorder_traversal
module nary_node
implicit none
private
public :: nary_node
type :: nary_node
integer :: val
type(nary_node), pointer, dimension(:) :: children
type(nary_node), pointer :: next
end type nary_node
end module nary_node
program main
use nary_tree_preorder_traversal
use nary_node
implicit none
integer, parameter :: n = 10
integer, parameter :: m = 1000
integer :: i, j
type(nary_node), pointer :: root
integer, allocatable :: preorder(:)
! Create a root node
allocate(root)
root%val = 1
! Create a list of children for the root node
allocate(root%children(n))
do i = 1, n
allocate(root%children(i)%children(m))
do j = 1, m
root%children(i)%children(j)%val = i*j
end do
end do
! Print the preorder traversal of the n-ary tree
preorder = preorder_traversal(root)
do i = 1, size(preorder)
write (*,*) preorder(i)
end do
end program main
temp.f95:4:38: 4 | public :: nary_tree_preorder_traversal | 1 Error: PUBLIC attribute applied to MODULE nary_tree_preorder_traversal at (1) temp.f95:29:20: 29 | type(nary_node), pointer, intent(in) :: root | 1 Error: Derived type ‘nary_node’ at (1) is being used before it is defined temp.f95:30:20: 30 | type(nary_node), pointer :: current | 1 Error: Derived type ‘nary_node’ at (1) is being used before it is defined temp.f95:35:33: 35 | allocate(preorder(size(root%children))) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:41:25: 41 | do i = 1, size(root%children) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:43:31: 43 | preorder(i) = current%val | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:46:32: 46 | if (associated(current%children)) then | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:47:56: 47 | preorder(i+1:) = preorder(i+1:) // current%children | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:48:11: 48 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:51:28: 51 | current => current%next | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:52:7: 52 | end do | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:8:32: 8 | function preorder_traversal(root) result(preorder) | 1 Error: Symbol ‘root’ at (1) has no IMPLICIT type temp.f95:38:11: 38 | current => root | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:61:19: 61 | public :: nary_node | 1 Error: PUBLIC attribute applied to MODULE nary_node at (1) temp.f95:63:17: 63 | type :: nary_node | 1 Error: MODULE attribute of ‘nary_node’ conflicts with PROCEDURE attribute at (1) temp.f95:65:20: 58 | module nary_node | 2 ...... 65 | type(nary_node), pointer, dimension(:) :: children | 1 Error: Type name ‘nary_node’ at (1) conflicts with previously declared entity at (2), which has the same name temp.f95:66:20: 58 | module nary_node | 2 ...... 66 | type(nary_node), pointer :: next | 1 Error: Type name ‘nary_node’ at (1) conflicts with previously declared entity at (2), which has the same name temp.f95:67:3: 67 | end type nary_node | 1 Error: Expecting END MODULE statement at (1) temp.f95:72:9: 72 | use nary_tree_preorder_traversal | 1 Fatal Error: Cannot open module file ‘nary_tree_preorder_traversal.mod’ for reading at (1): No such file or directory compilation terminated.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
def preorder(root):
result = []
if not root:
return result
nodes = [root]
while nodes:
current = nodes.pop()
result.append(current.val)
nodes.extend(reversed(current.children))
return result
The problem asks for an iterative solution, although a recursive solution would be trivial. Therefore, this solution uses a stack data structure to perform the preorder traversal of the n-ary tree.
result
to store the traversed node values.root
is null, return the empty result
.nodes
and push root
onto the stack.current
.
b. Add current
's value to the result
.
c. Reverse iterate through current
's children, pushing them onto the stack.result
.The algorithm's time complexity is O(n), where n is the number of nodes in the tree. This is because each node gets pushed onto and popped from the stack exactly once. The space complexity is also O(n), as at worst, the stack will store all the nodes in the tree.
#include <vector>
class Node {
public:
int val;
std::vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, std::vector<Node*> _children) {
val = _val;
children = _children;
}
};
vector<int> preorder(Node* root) {
vector<int> result;
if (root == nullptr) return result;
stack<Node*> nodes;
nodes.push(root);
while (!nodes.empty()) {
Node* current = nodes.top();
nodes.pop();
result.push_back(current->val);
for (int i = current->children.size() - 1; i >= 0; i--) {
nodes.push(current->children[i]);
}
}
return result;
}
The problem asks for an iterative solution, although a recursive solution would be trivial. Therefore, this solution uses a stack data structure to perform the preorder traversal of the n-ary tree.
result
to store the traversed node values.root
is null, return the empty result
.nodes
and push root
onto the stack.current
.
b. Add current
's value to the result
.
c. Reverse iterate through current
's children, pushing them onto the stack.result
.The algorithm's time complexity is O(n), where n is the number of nodes in the tree. This is because each node gets pushed onto and popped from the stack exactly once. The space complexity is also O(n), as at worst, the stack will store all the nodes in the tree.