TheAlgorithms-Ruby/data_structures/binary_trees/invert.rb
2020-05-14 11:05:14 +09:00

20 lines
No EOL
392 B
Ruby

# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {TreeNode}
def invert_tree(root)
if root == nil
return nil
end
temp = root.left
root.left = invert_tree(root.right)
root.right = invert_tree(temp)
return root
end