Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
import sys

######----code----######
class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

    def insert(self, node, data):
        while True:
            if node is None:
                node = Node(data)
                return
            else:
                if node.data < data:
                    if node.right is None:
                        node.right = Node(data)
                        return
                    else:
                        node = node.right
                elif node.data > data:
                    if node.left is None:
                        node.left = Node(data)
                        return
                    else:
                        node = node.left
                else:
                    break


def check(x, counter):
    if x == None: return 0
    depth_left = check(x.left, counter)
    depth_right = check(x.right, counter)
    if abs(depth_right - depth_left) >= 2: counter[0] += 1
    return 1 + max(depth_left, depth_right)


ans = [0]
root = Node(int(sys.stdin.readline()))

while True:
    try:
        root.insert(root, int(sys.stdin.readline()))
    except:
        break

check(root, ans)
sys.stdout.write('So node bi mat can bang la ' + str(ans[0]))