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. git一些有用的命令

    更改本地和远程分支的名称 git branch -m old_branch new_branch # Rename branch locally 本地分支改名 git push origin :old ...

  2. 剑指Offer 11. 二进制中1的个数 (其他)

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 题目地址 https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040e ...

  3. echarts环形图自动定位radius

    根据后台返回数据条数进行pie图radius定位:     var a = 100; var b = 0; var c = 0; var radius = []; for (var i in data ...

  4. JDBC ---获取数据字段 -- 转成map

    getConn = JdbcDataBaseUtil.getConnection(user,pwd,serverUrl,mysqDriver); //建立一个结果集,用来保存查询出来的结果 Resul ...

  5. 学习笔记TF017:自然语言处理、RNN、LSTM

    自然语言处理 (NLP)问题都是序列化的.前馈神经网络,在单次前馈中对到来数据处理,假定所有输入独立,模式丢失.循环神经网络(recurrent neural network,RNN)对时间显式建模神 ...

  6. too many open files(打开的文件过多)解决方法

    https://blog.csdn.net/roy_70/article/details/78423880 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...

  7. hsdfz -- 6.18 -- day3

    第三次被hn菜和hn话支配…… 相比起前两天好一点,但是由于前面时间安排的太散(睡着了……)导致c题DP差一点肝出来(所以最后没有…… 恩就算肝出来DP也只有30分,这次好歹是有DP思路了,继续康复吧 ...

  8. _Bool and bool

    _Bool is the defined before C99. bool has been defined in C99. bool is an alias for _Bool if you inc ...

  9. 账户和联系人 Accounts and Contacts 译

    原文链接: https://crmbook.powerobjects.com/basics/searching-and-navigation/understanding-accounts-and-co ...

  10. 第1章 Java语言概述--HelloWorld--环境搭建

    SE学什么 第1章 Java语言概述 第2章 基本语法 第3章 数组 第4章 面向对象编程(上) 第5章 面向对象编程(中) 第6章 面向对象编程(下) 第7章 异常处理 第8章 枚举类&注解 ...