#!/usr/bin/env python
# -*- coding: utf-8 -*-

from solution import TreeNode
from solution import BSTIterator

def constructOne(s):
    s = s.strip()
    if s == '#':
        return None
    else:
        return TreeNode(int(s))

def createTree(tree):
    q = []
    tree = tree.split(",")
    root = constructOne(tree[0]);
    q.append(root);
    idx = 1;
    while q:
        tn = q.pop(0)
        if not tn:
            continue
        if idx == len(tree):
            break
        left = constructOne(tree[idx])
        tn.left = left
        q.append(left)
        idx += 1
        if idx == len(tree):
            break
        right = constructOne(tree[idx])
        idx += 1
        tn.right = right
        q.append(right)
    return root

def printNode(tn, indent):
    sb = ""
    for i in range(indent):
        sb += "\t"
    sb += str(tn.val)
    print(sb)

def printTree(root, indent):
    if not root:
        return
    printTree(root.right, indent + 1)
    printNode(root, indent)
    printTree(root.left, indent + 1)

#  root = createTree("1, 2, 5, 3, 4, #, 6")
root = createTree("4, 3, 5, 2, #, #, 7")
i, v = BSTIterator(root), []
while i.hasNext():
    v.append(i.next())
for node in v:
    print(node.val)
