You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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