class Solution(object):
def __init__(self):
self.List = list() def rdfs(self,S):
if S != '':
length = len(S)
depth = len(self.List)
tempval = ''
tempdepth = 0
for i in range(length):
s = S[i]
if s != '-':
tempval += s
if i == length - 1:
while depth != tempdepth:
self.List.pop(-1)
depth = len(self.List)
parent = self.List[-1] val = int(tempval)
t = TreeNode(val)
if parent.left == None:
parent.left = t
elif parent.right == None:
parent.right = t else:
if tempval != '':
while depth != tempdepth:
self.List.pop(-1)
depth = len(self.List)
parent = self.List[-1] val = int(tempval)
t = TreeNode(val)
if parent.left == None:
parent.left = t
self.List.append(t)
self.rdfs(S[i:])
elif parent.right == None:
parent.right = t
self.List.append(t)
self.rdfs(S[i:])
break
else:
tempdepth += 1
else:
return None def recoverFromPreorder(self, S: str) -> 'TreeNode':
tempval = ''
length = len(S)
for i in range(length):
s = S[i]
if s != '-':#数字
tempval += s
if i == length - 1:
val = int(tempval)
root = TreeNode(val) else:#遇到横线,数字结束
val = int(tempval)
root = TreeNode(val)
self.List.append(root)
self.rdfs(S[i:])
self.List.pop(-1)
return root
return root

leetcode1028的更多相关文章

  1. [Swift]LeetCode1028. 从先序遍历还原二叉树 | Recover a Tree From Preorder Traversal

    We run a preorder depth first search on the root of a binary tree. At each node in this traversal, w ...

  2. leetcode1028 从先序遍历还原二叉树 python 100%内存 一次遍历

    1028. 从先序遍历还原二叉树 python 100%内存 一次遍历     题目 我们从二叉树的根节点 root 开始进行深度优先搜索. 在遍历中的每个节点处,我们输出 D 条短划线(其中 D 是 ...

随机推荐

  1. day 06 字符串和列表的方法

    一.整形int 定义方式: age=18    #调用age=int(18)的方法,自动调用 n=int("123") #只能转换纯数字类型 二:浮点型float 定义方式 sal ...

  2. Windows10 VS2017 C++信号处理

    #include "pch.h" #include <iostream> #include <csignal> #include <windows.h ...

  3. js 点击下载文件

    下载的文件类型如果浏览器不能打开会直接下载,能打开的需要后台在响应头部进行设定. Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的 ...

  4. Lua IDE工具-Intellij IDEA+lua插件配置教程(Chianr出品)

    Lua 编译工具IDE-Intellij IDEA 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Ch ...

  5. python gevent自动挡的协程切换。

    import gevent def func(): print('running func 111')#第一步运行 gevent.sleep(2)#切换到下个协程 print('running fun ...

  6. 导入导出Oracle

  7. webpack学习笔记(三)

    访问网址: https://github.com/webpack/analyse "scripts": { "dev-build": "webpack ...

  8. CSS hack 360浏览器 极速模式与兼容模式

    自动切换浏览器模式对于360浏览器7.1版本有效,8.1无效 <%@ Page Language="C#" AutoEventWireup="true" ...

  9. MySQL Error--Error Code

    mysql error code(备忘) 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导 ...

  10. easyUI默认图标的使用

    使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...