Given the root
of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3] Output: [1,3,2]
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 :: n = 3
type(tree_node), allocatable :: roots(:)[:]
integer :: i
allocate(roots(n)[*])
! Example 1
allocate(roots(1)[1])
allocate(roots(1)[1]%left, source=tree_node(-1))
allocate(roots(1)[1]%right, source=tree_node(2))
call print_inorder(roots(1)[1])
deallocate(roots(1)[1])
! Example 2
allocate(roots(1)[1])
allocate(roots(1)[1]%left, source=tree_node(-1))
allocate(roots(1)[1]%right, source=tree_node(-1))
call print_inorder(roots(1)[1])
deallocate(roots(1)[1])
! Example 3
allocate(roots(1)[1])
allocate(roots(1)[1]%left, source=tree_node(1))
allocate(roots(1)[1]%right, source=tree_node(2))
call print_inorder(roots(1)[1])
deallocate(roots(1)[1])
contains
subroutine print_inorder(root)
type(tree_node), intent(in) :: root
if (associated(root%left)) then
call print_inorder(root%left)
end if
write (unit=error_unit, fmt='(*(g0,1x))') root%value
if (associated(root%right)) then
call print_inorder(root%right)
end if
end subroutine print_inorder
end program main
type :: tree_node
real(kind=DP) :: value
class(tree_node), pointer :: left => null()
class(tree_node), pointer :: right => null()
end type tree_node
temp.f95:5:20: 5 | type(tree_node), allocatable :: roots(:)[:] | 1 Error: Derived type ātree_nodeā at (1) is being used before it is defined temp.f95:8:13: 8 | allocate(roots(n)[*]) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:11:13: 11 | allocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:12:13: 12 | allocate(roots(1)[1]%left, source=tree_node(-1)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:13:13: 13 | allocate(roots(1)[1]%right, source=tree_node(2)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:14:32: 14 | call print_inorder(roots(1)[1]) | 1 Error: Coarray designator at (1) but ārootsā is not a coarray temp.f95:15:21: 15 | deallocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is not a nonprocedure pointer nor an allocatable variable temp.f95:18:13: 18 | allocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:19:13: 19 | allocate(roots(1)[1]%left, source=tree_node(-1)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:20:13: 20 | allocate(roots(1)[1]%right, source=tree_node(-1)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:21:32: 21 | call print_inorder(roots(1)[1]) | 1 Error: Coarray designator at (1) but ārootsā is not a coarray temp.f95:22:21: 22 | deallocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is not a nonprocedure pointer nor an allocatable variable temp.f95:25:13: 25 | allocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:26:13: 26 | allocate(roots(1)[1]%left, source=tree_node(1)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:27:13: 27 | allocate(roots(1)[1]%right, source=tree_node(2)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:28:32: 28 | call print_inorder(roots(1)[1]) | 1 Error: Coarray designator at (1) but ārootsā is not a coarray temp.f95:29:21: 29 | deallocate(roots(1)[1]) | 1 Error: Allocate-object at (1) is not a nonprocedure pointer nor an allocatable variable temp.f95:34:24: 34 | type(tree_node), intent(in) :: root | 1 Error: Derived type ātree_nodeā at (1) is being used before it is defined temp.f95:35:29: 35 | if (associated(root%left)) then | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:36:37: 36 | call print_inorder(root%left) | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:37:11: 37 | end if | 1 Error: Expecting END SUBROUTINE statement at (1) temp.f95:38:56: 38 | write (unit=error_unit, fmt='(*(g0,1x))') root%value | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:39:29: 39 | if (associated(root%right)) then | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:40:37: 40 | call print_inorder(root%right) | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:41:11: 41 | end if | 1 Error: Expecting END SUBROUTINE statement at (1) temp.f95:1:12: 1 | program main | 1 ...... 46 | type :: tree_node | 2 Error: Two main PROGRAMs at (1) and (2)
module binary_tree_inorder_traversal
implicit none
private
public :: inorder_traversal
contains
function inorder_traversal(root) result(values)
type(node), pointer, intent(in) :: root
integer, allocatable :: values(:)
integer :: i, n
type(node), pointer :: current
! Find the number of nodes in the tree
n = 0
current => root
do while (associated(current))
n = n + 1
current => current%left
end do
! Allocate an array to store the values
allocate(values(n))
! Perform the inorder traversal
i = 1
current => root
do while (associated(current))
if (associated(current%left)) then
current => current%left
do while (associated(current))
values(i) = current%val
i = i + 1
current => current%right
end do
end if
values(i) = current%val
i = i + 1
current => current%right
end do
end function inorder_traversal
type node
integer :: val
type(node), pointer :: left
type(node), pointer :: right
end type node
end module binary_tree_inorder_traversal
program test
use binary_tree_inorder_traversal
implicit none
type(node), pointer :: root
integer, allocatable :: values(:)
! Example 1
root => node(1, null(), null())
root%left => node(2, null(), null())
root%right => node(3, null(), null())
values = inorder_traversal(root)
write (*,*) values
! Example 2
root => null()
values = inorder_traversal(root)
write (*,*) values
! Example 3
root => node(1, null(), null())
values = inorder_traversal(root)
write (*,*) values
end program test
temp.f95:9:15: 9 | type(node), pointer, intent(in) :: root | 1 Error: Derived type ānodeā at (1) is being used before it is defined temp.f95:13:15: 13 | type(node), pointer :: current | 1 Error: Derived type ānodeā at (1) is being used before it is defined temp.f95:20:28: 20 | current => current%left | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:30:32: 30 | if (associated(current%left)) then | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:31:32: 31 | current => current%left | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:33:37: 33 | values(i) = current%val | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:35:36: 35 | current => current%right | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:37:11: 37 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:38:29: 38 | values(i) = current%val | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:40:28: 40 | current => current%right | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:45:9: 45 | type node | 1 Error: Unexpected derived type declaration statement in CONTAINS section at (1) temp.f95:46:18: 46 | integer :: val | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:47:15: 47 | type(node), pointer :: left | 1 Error: Derived type ānodeā at (1) is being used before it is defined temp.f95:48:15: 48 | type(node), pointer :: right | 1 Error: Derived type ānodeā at (1) is being used before it is defined temp.f95:49:3: 49 | end type node | 1 Error: Expecting END MODULE statement at (1) temp.f95:8:31: 8 | function inorder_traversal(root) result(values) | 1 Error: Symbol ārootā at (1) has no IMPLICIT type temp.f95:17:11: 17 | current => root | 1 Error: Symbol ācurrentā at (1) has no IMPLICIT type temp.f95:54:5: 54 | use binary_tree_inorder_traversal | 1 Fatal Error: Cannot open module file ābinary_tree_inorder_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 inorderTraversalHelper(root, result):
if not root:
return
inorderTraversalHelper(root.left, result)
result.append(root.val)
inorderTraversalHelper(root.right, result)
def inorderTraversal(root):
result = []
inorderTraversalHelper(root, result)
return result
The algorithm simply performs an inorder traversal on the given binary tree. An inorder traversal goes as follows:
This process is implemented by defining a helper function (inorderTraversalHelper
) that takes a node and a result
list (or vector in C++), and appends the node's value to the result
list according to the inorder traversal order. The main inorderTraversal
function initializes the result list and calls the helper function with the root of the tree. At the end, inorderTraversal
returns the result list containing the values of the nodes in inorder traversal order.
#include<vector>
using namespace std;
class TreeNode {
public:
int val;
TreeNode *left;
TreeNode *right;
};
void inorderTraversalHelper(TreeNode* root, vector<int>& result) {
if (!root) return;
inorderTraversalHelper(root->left, result);
result.push_back(root->val);
inorderTraversalHelper(root->right, result);
}
vector<int> inorderTraversal(TreeNode* root){
vector<int> result;
inorderTraversalHelper(root, result);
return result;
}
The algorithm simply performs an inorder traversal on the given binary tree. An inorder traversal goes as follows:
This process is implemented by defining a helper function (inorderTraversalHelper
) that takes a node and a result
list (or vector in C++), and appends the node's value to the result
list according to the inorder traversal order. The main inorderTraversal
function initializes the result list and calls the helper function with the root of the tree. At the end, inorderTraversal
returns the result list containing the values of the nodes in inorder traversal order.