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

20 lines
432 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 {Integer}
def maximum_depth_of_binary_tree(tree_node)
if tree_node.nil?
0
else
1 + [maximum_depth_of_binary_tree(tree_node.left), maximum_depth_of_binary_tree(tree_node.right)].max
end
end