All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0104_maximum_depth_of_binary_tree.solution.rb Maven / Gradle / Ivy

# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
# #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree
# #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue
# #Big_O_Time_O(N)_Space_O(H) #2023_11_23_Time_55_ms_(96.72%)_Space_211.3_MB_(37.23%)

# Definition for a binary tree node.
# class TreeNode
#     attr_accessor :val, :left, :right
#     def initialize(val = 0, left = nil, right = nil)
#         @val = val
#         @left = left
#         @right = right
#     end
# end
# @param {TreeNode} root
# @return {Integer}
def max_depth(root)
  find_depth(root, 0)
end

private

def find_depth(node, current_depth)
  return 0 if node.nil?

  current_depth += 1
  1 + [find_depth(node.left, current_depth), find_depth(node.right, current_depth)].max
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy