Tuesday, July 26, 2016

get 2nd largest item in bst

(n):
    if n == null:
        throw
    max2 = null
    if n.right != null:
        max2 = n
        while n.right != null:
            max2 = n
            n = n.right
    else if n.left != null:
        max2 = n.left
        while max2.right != null:
            max2 = max2.right
    return max2
[Hat tip to IC]

No comments:

Post a Comment