Simple Ruby solution using standard In Order traversal


  • 0
    T
    def inorder_successor(root, p)
        @found = false
        @successor = nil
        find_node(root, p)
        return @successor
    end
    
    def find_node(node, p)
        return if node.nil? || @successor
        find_node(node.left, p)
        if @found
            @successor ||= node 
            return
        end
        @found = true if node.val == p.val
        find_node(node.right, p)
    end
    

Log in to reply
 

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