• + 11 comments

    I came up with a similar solution in Java :) So far, I like it the most.

    void top_view(Node root)
    {
        top_view(root, 0);
    }
    
    void top_view(Node root, int side)
    {
        if (root != null) {
            if (side <= 0) {
                top_view(root.left, -1);
            }
            
            System.out.print(root.data + " ");
            
            if (side >= 0) {
                top_view(root.right, 1);
            }
        }
    }