• + 15 comments

    C solution using recursion without any additional functions with time complexity of O(N) and 0 space complexity. It's the shortest solution on this forum, but it does alter the tree.

    void top_view(node * root) {
    
        if (root->left) {
            root->left->right = NULL;
            top_view(root->left);
        }
    
        printf("%d ", root->data);
    
        if (root->right) {
            root->right->left = NULL;
            top_view(root->right);
        }
    
    }