root ,返回 它的 前序 遍历 。[7, 9, 4, null, null, 5, 6, 8, 1, 2, 6, 9, null, 6, 6, 8]
[7, 9, 4, 5, 8, 8, 1, 6, 2, 6]
import json
class Node:
def __init__(self, data):
self.value = data
self.left = None
self.right = None
def __repr__(self):
return "<Node val=%s left=%s right=%s>" % (self.value, self.left,
self.right)
st = json.loads(input())
head = Node(st.pop(0))
l = [head]
while st:
cnt = len(l) * 2
lc = []
while st and cnt:
cnt -= 1
lc.append(Node(n) if (n := st.pop(0)) else None)
j = 0
for i in l:
if i and j < len(lc):
i.left = lc[j]
j += 1
if j < len(lc):
i.right = lc[j]
j += 1
l = lc
...
...