“Pohon: tampilan atas” Kode Jawaban

Pohon: tampilan atas

def topView(root):
    d = {}
    def traverse(root, key, level):
        if root:
            if key not in d:
                d[key] = [root, level]
            elif d[key][1] > level:
                d[key] = [root, level]           
            
            traverse(root.left, key-1, level +1)
            traverse(root.right, key+1, level + 1)
            
    traverse(root, 0, 0)
    for key in sorted(d):
        print(d[key][0], end = " ")
Grieving Gazelle

Pohon: tampilan atas

def topView(root):
    d = {}
    def traverse(root, key, level):
        if root:
            if key not in d:                # when new key is added
                d[key] = [root, level]
            elif d[key][1] > level:         # for movement inside tree
                d[key] = [root, level]           
            
            traverse(root.left, key-1, level +1)
            traverse(root.right, key+1, level + 1)
            
    traverse(root, 0, 0)
    for key in sorted(d):
        print(d[key][0], end = " ")
Grieving Gazelle

Jawaban yang mirip dengan “Pohon: tampilan atas”

Pertanyaan yang mirip dengan “Pohon: tampilan atas”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya