recursive ruby solution


  • 0
    L
    # starting at the root, we think:
    # what is the maximum depth of the r subtree ?
    # what is the maximum depth of the l subtree ?
    # whichever is larger, take that value and + 1 (root node)
    def max_depth(root)
        return 0 unless root
        l = max_depth(root.left)
        r = max_depth(root.right)
        l > r ? l + 1 : r + 1
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.