Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
587 B
3
Indexable
class Node:
    def __init__(self, value=None, children=None):
        self.value = value
        self.children = children or []

def parse_tree(string):
    stack = [Node()]
    for char in string:
        if char == '(':
            node = Node()
            stack[-1].children.append(node)
            stack.append(node)
        elif char == ')':
            stack.pop()
        elif char.isdigit():
            stack[-1].value = int(char)
    return stack[0].children[0] if stack[0].children else None

string = "(1(2)(3)(2))"
tree = parse_tree(string)
print(tree.children[2].value)