TheAlgorithms-Ruby/data_structures/binary_trees/invert.rb

20 lines
392 B
Ruby
Raw Normal View History

2017-10-02 01:00:27 +02:00
# Definition for a binary tree node.
# class TreeNode
2020-05-14 04:05:14 +02:00
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
2017-10-02 01:00:27 +02:00
# end
# @param {TreeNode} root
# @return {TreeNode}
def invert_tree(root)
2020-05-14 04:05:14 +02:00
if root == nil
return nil
end
temp = root.left
root.left = invert_tree(root.right)
root.right = invert_tree(temp)
return root
2017-10-02 01:00:27 +02:00
end