yeonghwanjeon / coding_practice

0 stars 0 forks source link

[HackerRank]Tree Level Order Traversal(완료) #40

Open yeonghwanjeon opened 5 years ago

yeonghwanjeon commented 5 years ago

class BinarySearchTree: def init(self): self.root = None

def create(self, val):  
    if self.root == None:
        self.root = Node(val)
    else:
        current = self.root

        while True:
            if val < current.info:
                if current.left:
                    current = current.left
                else:
                    current.left = Node(val)
                    break
            elif val > current.info:
                if current.right:
                    current = current.right
                else:
                    current.right = Node(val)
                    break
            else:
                break

""" Node is defined as self.left (the left child of the node) self.right (the right child of the node) self.info (the value of the node) """ def levelOrder(root):

Write code Here

queue = [root]
s = ""
while queue :
    t = queue.pop(0)
    s = s + " " + str(t.info)

    if t.left is not None :
        queue.append(t.left)

    if t.right is not None :
        queue.append(t.right)

print(s.strip())