yeonghwanjeon / coding_practice

0 stars 0 forks source link

[HackerRank]Tree Height of a Binary Tree (완료) #39

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

Enter your code here. Read input from STDIN. Print output to STDOUT

''' class Node: def init(self,info): self.info = info
self.left = None
self.right = None

   // this is a node of the tree , which contains info as data, left , right

''' def height(root): if root is None : return 0

queue = [(0, root)]
h = 0
while queue :
    th, t = queue.pop(0)
    h = max(h, th)

    if t.left is not None :
        lh = th + 1
        queue.append((lh, t.left))

    if t.right is not None :
        rh = th + 1
        queue.append((rh, t.right))

return h