programming-examples/ruby/Algorithms/invert_binary_tree.rb
2019-11-15 12:59:38 +01:00

17 lines
386 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_binary_tree(root)
root.right, root.left = invert_binary_tree(root.left), invert_binary_tree(root.right) unless root.nil?
root
end