Sort by

recency

|

967 Discussions

|

  • + 1 comment

    Agree the problem explanation is bad, had to google it. Basically we can think of it as root is at 0 on the x axis then the left child is at -1 and right child is at 1. If a node is at the same place on the x axis and there is another node higher up the tree/ closer to the root, it is blocked.

  • + 0 comments

    This problem is simply not well defined. "the set of nodes visible when the tree is viewed from the top" is meaningless. There is no "top" though I suppose they mean "root". Similarly "viewed" simply does not mean anything in this context.

  • + 0 comments

    Hi, I'm having an issue to run the code i did in this exercise. I tested on my machine and it worked as expected. But for some reason, the test here does not output any value. Am I doing something wrong?

    def topView(root):
        left_wing = ''
        right_wing = ''
        if(root.left is not None):
            left_wing = topView(root.left)
        if(root.right is not None):
            right_wing = topView(root.right)
            
        ''' return value if is a leaf '''
        if((root.left is None) & (root.right is None)):
            return str(root.info)
        ''' If it has only the right side '''
        if(left_wing == ''):
            return str(root.info) +' '+ right_wing
        ''' If it has only the left side '''
        if(right_wing == ''):
            return str(root.info) +' '+ left_wing
        ''' Else then check between both wings'''
        rw_max = int((right_wing.split(' '))[-1])
        lw_max = int((left_wing.split(' '))[-1])
        
        if(rw_max>=lw_max):
            return str(root.info) +' '+ right_wing
        else:
            return str(root.info) +' '+ left_wing
    
  • + 0 comments

    I try to find a better definition of top view since the one presented is terrible: the list nodes, from left to the right, that projected vertically from the top to the bottom, are NOT covered by other nodes.

    # layer: meant as horizontal layer, integer
    #    < 0 (left of the root) or > 0 (right of the root)
    # depth: depth in the three from zero, always positive
    
    def explore(node, results, layer: int = 0, depth : int = 0):
        if layer not in results:
            # if it a new layer, no matter of the current depth
            results[layer] = { 'depth': depth, 'content': node.info }
        else:
            # there was already a node on this layer
            if depth < results[layer].get('depth'):
                results[layer] = { 'depth': depth, 'content': node.info }
        
        if node.left is not None:
            explore(node.left, results, layer-1, depth+1)
           
        if node.right is not None:
            explore(node.right, results, layer+1, depth+1)
        
    def topView(root):
        #Write your code here
        results = {}
        explore(root, results)
    
        min_index = min(results.keys())
        max_index = max(results.keys())
        for i in range(min_index, max_index+1):
            print(results[i].get('content'), end=" ")
    
  • + 0 comments

    The definition of top view is not clear at all. There should be at least two examples with expected output to help understand a better description.