programming-examples/ruby/Algorithms/invert_binary_tree.rb

17 lines
386 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# 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