LeetCode #1028

Recover a Tree From Preorder Traversal

1개의 풀이 · Python

문제 원문 보기 ↗

SOLUTION INFO

Python · main.py

main.py
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
        V = list(map(int, [x for x in traversal.split('-') if x]))
        D = [0]
        push = True
        for ch in traversal:
            if ch == '-':
                if push:
                    D.append(0)
                    push = False
                D[-1] += 1
            else:
                push = True
        root = TreeNode(V[0])
        idx = 1
        st = [(root, 0)]
        while st and idx < len(V):
            while st and st[-1][1] >= D[idx]: st.pop(-1)
            if not st: break
            cur, d = st[-1]
            if cur.left is None:
                cur.left = TreeNode(V[idx])
                st.append((cur.left, D[idx]))
            elif cur.right is None:
                cur.right = TreeNode(V[idx])
                st.append((cur.right, D[idx]))
            idx += 1
        return root

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.