Given the root
of a binary tree, invert the tree, and return its root.
Example 1:
Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1]
Example 2:
Input: root = [2,1,3] Output: [2,3,1]
Example 3:
Input: root = [] Output: []
Constraints:
[0, 100]
.-100 <= Node.val <= 100
program main
! Solve the problem described in the statement
implicit none
type :: TreeNode
integer :: val
type(TreeNode), pointer :: left => null(), right => null()
end type TreeNode
type(TreeNode), target :: root
type(TreeNode), pointer :: current
integer :: i
! Examples
call invertTree(root)
print "(A)", "Example 1:"
do i = 1, size(root%left)
print "(I0)", root%left(i)%val
end do
print *, ""
do i = 1, size(root%right)
print "(I0)", root%right(i)%val
end do
print *, ""
call invertTree(root)
print "(A)", "Example 2:"
do i = 1, size(root%left)
print "(I0)", root%left(i)%val
end do
print *, ""
do i = 1, size(root%right)
print "(I0)", root%right(i)%val
end do
print *, ""
call invertTree(root)
print "(A)", "Example 3:"
do i = 1, size(root%left)
print "(I0)", root%left(i)%val
end do
print *, ""
do i = 1, size(root%right)
print "(I0)", root%right(i)%val
end do
print *, ""
contains
subroutine invertTree(root)
! Invert the tree and return its root
implicit none
type(TreeNode), intent(inout) :: root
type(TreeNode), pointer :: current
type(TreeNode), pointer :: temp
if (associated(root)) then
current => root
do while (associated(current))
! Swap the left and right pointers
temp => current%left
current%left => current%right
current%right => temp
! Recursively invert the left and right subtrees
call invertTree(current%left)
call invertTree(current%right)
! Move to the next node
current => current%right
end do
end if
end subroutine invertTree
end program main
temp.f95:18:32: 18 | print "(I0)", root%left(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:22:33: 22 | print "(I0)", root%right(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:29:32: 29 | print "(I0)", root%left(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:33:33: 33 | print "(I0)", root%right(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:40:32: 40 | print "(I0)", root%left(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:44:33: 44 | print "(I0)", root%right(i)%val | 1 Error: Syntax error in PRINT statement at (1) temp.f95:57:23: 57 | if (associated(root)) then | 1 Error: ‘pointer’ argument of ‘associated’ intrinsic at (1) must be a POINTER temp.f95:58:23: 58 | current => root | 1 Error: Pointer assignment target is neither TARGET nor POINTER at (1) temp.f95:66:45: 66 | call invertTree(current%left) | 1 Error: SUBROUTINE ‘inverttree’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:67:46: 67 | call invertTree(current%right) | 1 Error: SUBROUTINE ‘inverttree’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:17:19: 17 | do i = 1, size(root%left) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:21:19: 21 | do i = 1, size(root%right) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:28:19: 28 | do i = 1, size(root%left) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:32:19: 32 | do i = 1, size(root%right) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:39:19: 39 | do i = 1, size(root%left) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array temp.f95:43:19: 43 | do i = 1, size(root%right) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array
!InvertBinaryTree
!This function takes a pointer to the root of a binary tree and returns a pointer to the inverted tree.
!Definition of the Node structure:
type Node
integer :: val
type(Node), pointer :: left
type(Node), pointer :: right
end type Node
interface
function invertTree(root) result(newRoot)
type(Node), pointer :: root
type(Node), pointer :: newRoot
end function invertTree
end interface
contains
function invertTree(root) result(newRoot)
type(Node), pointer :: root
type(Node), pointer :: newRoot
type(Node), pointer :: current
type(Node), pointer :: newCurrent
!Create a new root node
allocate(newRoot)
newRoot%val = root%val
newRoot%left => null()
newRoot%right => null()
!Traverse the tree and invert the links
current => root
do while (associated(current))
!Create a new node for the current node
allocate(newCurrent)
newCurrent%val = current%val
newCurrent%left => null()
newCurrent%right => null()
!Invert the links
if (associated(current%left)) then
newCurrent%right => invertTree(current%left)
end if
if (associated(current%right)) then
newCurrent%left => invertTree(current%right)
end if
!Update the current node
current => newCurrent
end do
!Return the inverted tree
newRoot => current
end function invertTree
end
!Test the function with the given examples
program test
implicit none
!Declare the variables
type(Node), pointer :: root
type(Node), pointer :: newRoot
!Create the root node
allocate(root)
root%val = 4
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
write (*,*) "Inverted tree: ", newRoot%val
!Free the memory
deallocate(root)
deallocate(newRoot)
!Create the root node
allocate(root)
root%val = 2
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
write (*,*) "Inverted tree: ", newRoot%val
!Free the memory
deallocate(root)
deallocate(newRoot)
!Create the root node
allocate(root)
root%val = 1
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
write (*,*) "Inverted tree: ", newRoot%val
!Free the memory
deallocate(root)
deallocate(newRoot)
!Create the root node
allocate(root)
root%val = 3
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
write (*,*) "Inverted tree: ", newRoot%val
!Free the memory
deallocate(root)
deallocate(newRoot)
!Create the root node
allocate(root)
root%val = 6
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
write (*,*) "Inverted tree: ", newRoot%val
!Free the memory
deallocate(root)
deallocate(newRoot)
!Create the root node
allocate(root)
root%val = 9
root%left => null()
root%right => null()
!Invert the tree
newRoot => invertTree(root)
!Print the result
temp.f95:14:19: 14 | type(Node), pointer :: root | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:15:19: 15 | type(Node), pointer :: newRoot | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:21:25: 21 | function invertTree(root) result(newRoot) | 1 Error: Symbol ‘inverttree’ at (1) already has an explicit interface temp.f95:22:31: 22 | type(Node), pointer :: root | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:34: 23 | type(Node), pointer :: newRoot | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:34: 24 | type(Node), pointer :: current | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:37: 25 | type(Node), pointer :: newCurrent | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:28:13: 28 | allocate(newRoot) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:29:13: 29 | newRoot%val = root%val | 1 Error: Symbol ‘newroot’ at (1) has no IMPLICIT type temp.f95:30:13: 30 | newRoot%left => null() | 1 Error: Symbol ‘newroot’ at (1) has no IMPLICIT type temp.f95:31:13: 31 | newRoot%right => null() | 1 Error: Symbol ‘newroot’ at (1) has no IMPLICIT type temp.f95:34:19: 34 | current => root | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:35:34: 35 | do while (associated(current)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:37:17: 37 | allocate(newCurrent) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:38:20: 38 | newCurrent%val = current%val | 1 Error: Symbol ‘newcurrent’ at (1) has no IMPLICIT type temp.f95:39:20: 39 | newCurrent%left => null() | 1 Error: Symbol ‘newcurrent’ at (1) has no IMPLICIT type temp.f95:40:20: 40 | newCurrent%right => null() | 1 Error: Symbol ‘newcurrent’ at (1) has no IMPLICIT type temp.f95:43:32: 43 | if (associated(current%left)) then | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:44:24: 44 | newCurrent%right => invertTree(current%left) | 1 Error: Symbol ‘newcurrent’ at (1) has no IMPLICIT type temp.f95:45:11: 45 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:46:32: 46 | if (associated(current%right)) then | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:47:24: 47 | newCurrent%left => invertTree(current%right) | 1 Error: Symbol ‘newcurrent’ at (1) has no IMPLICIT type temp.f95:48:11: 48 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:51:29: 51 | current => newCurrent | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:52:7: 52 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:55:22: 55 | newRoot => current | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:56:3: 56 | end function invertTree | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:6:9: 6 | type Node | 1 ...... 61 | program test | 2 Error: Two main PROGRAMs at (1) and (2)
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def invert_tree(root):
if root is None:
return None
root.left, root.right = invert_tree(root.right), invert_tree(root.left)
return root
This recursive algorithm traverses the tree in depth-first search order. At each visited node, we swap its left child with its right child. Then we apply the same process for the children.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) {
return nullptr;
}
TreeNode* temp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(temp);
return root;
}
This recursive algorithm traverses the tree in depth-first search order. At each visited node, we swap its left child with its right child. Then we apply the same process for the children.