mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-29 22:24:07 +01:00
19 lines
375 B
Ruby
19 lines
375 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)
|
|
return nil if root.nil?
|
|
|
|
temp = root.left
|
|
root.left = invert_tree(root.right)
|
|
root.right = invert_tree(temp)
|
|
root
|
|
end
|