Sort by

recency

|

970 Discussions

|

  • + 0 comments

    Here is Tree: Top View solution in Python, Java, C++ and c programming - https://programmingoneonone.com/hackerrank-tree-top-view-problem-solution.html

  • + 0 comments

    Editorial solution complicates the problem into a whole new dimension. This BFS of mine is more intuitive, efficient and faster. Hope this helps :)

    Output Buffer Logic: - According to the constraint, there are exactly 500 nodes in the tree in the worst case.

    So, I created a raw array where negative "horizontal distance" vlaues will be placed from index 0 to 499 and positive values will be placed from index 499 to 999.

    Then I did simple index mapping arithmetic and get the job done. That's all.

    //BFS is required 
        void topView(Node * root) {
            int arr[1000]; memset(arr, 0, sizeof(arr)); 
            queue<pair<Node*, int>> q; 
            q.push(make_pair(root, 0)); 
            int neg = 0, pos = 0; 
            
            arr[499] = root->data; 
            
            Node* tmp; int hd; 
            while(!q.empty()) {
                tmp = q.front().first; 
                hd = q.front().second; 
                
                q.pop(); 
                
                if(tmp->left) {
                    q.push(make_pair(tmp->left, hd - 1)); 
                    if(hd - 1 < neg) {
                        arr[499 + --neg] = tmp->left->data; 
                    }
                }
                
                if(tmp->right) {
                    q.push(make_pair(tmp->right, hd + 1)); 
                    if(hd + 1 > pos) {
                        arr[499 + ++pos] = tmp->right->data;                           
                    }
                }
            }
            
            for(int i = 0; i < 1000; ++i) {
                if(arr[i]) cout << arr[i] << ' '; 
            }
        }
    
  • + 0 comments

    In Java 15 , I can neither create Node class on my own nor there is anything defined . I guess platform needs to correct it

  • + 1 comment

    Terrible question. Doesn't provide an example for the left side and the logical correct answer is somehow wrong.

  • + 0 comments

    Python code:

    from collections import deque

    def topView(root):

    #Write your code here
    
    if not root:
    
        return []
    
    # Dictionary to store the first node at each horizontal distance
    
    top_view_map = {}
    
    # Queue for BFS: (node, horizontal_distance)
    
    queue = deque([(root, 0)])
    
    while queue:
    
        node, hd = queue.popleft()
    
        # If this horizontal distance is seen for the first time, record the node
    
        if hd not in top_view_map:
    
            top_view_map[hd] = node.info
    
        # Add left and right children to the queue with updated horizontal distances
    
        if node.left:
    
            queue.append((node.left, hd - 1))
    
        if node.right:
    
            queue.append((node.right, hd + 1))
    
    
    for hd in sorted(top_view_map.keys()):
        print(top_view_map[hd], end = " ")