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. [Version Control]—— Git如何使用

    Git是什么? Git是目前世界上最先进的分布式版本控制系统. 它没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上.既然每个人的电脑都有一 ...

  2. JavaScript之Math

    1. Math Math是js的内置函数,无需创建,把Math作为对象使用就可以调用其属性和方法. 2.Date    2.1 日期对象创建 var now = new Date ( ); 含参数时表 ...

  3. ipa 注入 dylib

    前些日子再github找到了一个内存修改器 DLGMemor 免越狱在app内植入修改器,感觉很不错,就尝试去看看是否可行. 用到的工具:  Xcode 10. optool 首先要做的,安装 opt ...

  4. Session & Cookie小知识~

    Cookie 一个HTTP cookie的(也称为网络Cookie,互联网的cookie,浏览器cookie,或者干脆饼干)是一小块从发送的数据的网站用户的并存储在用户的计算机上的网页浏览器,而用户浏 ...

  5. 【java编程】java中的移位运算符

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  6. 算法笔记 3.2 codeup1935 查找学生信息

    #include <stdio.h> #include <string.h> const int maxn = 1e3; struct student{ ]; ]; //!!! ...

  7. kafka原理和实践(五)spring-kafka配置详解

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  8. c#读sql server数据添加到MySQL数据库

    using System;using System.Collections.Generic;using System.Text;using Console = System.Console;using ...

  9. Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器

    Spring Cloud 为 HTTP 请求的各个阶段提供了多个过滤器,这些过滤器的执行顺序由各自提供的一个 int 值决定,提供的值越小则优先级越高,默认的过滤器及优先级如下: 自定义过滤器 在默认 ...

  10. unity的一些tips

    主要是我知乎上回答的一个关于unity的tip,备忘. 说说我所看到unity相关的,不好的习惯: 1 尽量不要在Awake(), start()等函数内加入业务逻辑的初始化代码.首先无法简便的直接启 ...