-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecoveraTreeFromPreorderTraversal.py
More file actions
34 lines (32 loc) · 1019 Bytes
/
recoveraTreeFromPreorderTraversal.py
File metadata and controls
34 lines (32 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/
# Author: Miao Zhang
# Date: 2021-04-05
# 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, S: str) -> TreeNode:
path, pos = list(), 0
while pos < len(S):
d = 0
while S[pos] == '-':
d += 1
pos += 1
val = 0
while pos < len(S) and S[pos].isdigit():
val = 10 * val + (ord(S[pos]) - ord('0'))
pos += 1
node = TreeNode(val)
if d == len(path):
if path:
path[-1].left = node
else:
path = path[:d]
path[-1].right = node
path.append(node)
return path[0]