【leetcode】1028. 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, we output
D
dashes (whereD
is the depth of this node), then we output the value of this node. (If the depth of a node isD
, the depth of its immediate child isD+1
. The depth of the root node is0
.)If a node has only one child, that child is guaranteed to be the left child.
Given the output
S
of this traversal, recover the tree and return itsroot
.Example 1:
Input: "1-2--3--4-5--6--7"
Output: [1,2,5,3,4,6,7]Example 2:
Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]Example 3:
Input: "1-401--349---90--88"
Output: [1,401,null,349,88,90]Note:
- The number of nodes in the original tree is between
1
and1000
.- Each node will have a value between
1
and10^9
.
解题思路:本题就是DFS的思想。首先解析Input,得到每个数值所对应的层级,接下来把Input中每个元素创建成树的节点,并且依次存入stack中。每次从Input新取出一个元素,判断其层级是否是stack中最后一个元素的层级加1,如果是表示这个节点是stack中最后一个元素的左子节点;如果是stack中倒数第二个元素的层级加1,如果是表示这个节点是stack中倒数第二个元素的右子节点;如果都不满足,stack中最后一个元素出栈,再继续做如上判断,直到找出其父节点为止。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def recoverFromPreorder(self, S):
"""
:type S: str
:rtype: TreeNode
"""
queue = [[0]]
dashCount = 0
val = ''
for i in S:
if i == '-':
if len(val) > 0:
queue[-1].insert(0,val)
val = ''
dashCount += 1
else:
if dashCount > 0:
queue.append([dashCount])
dashCount = 0
val += i
queue[-1].insert(0, val)
#print queue item = queue.pop(0)
root = TreeNode(int(item[0]))
nodeList = [[root,item[1]]]
while len(queue) > 0:
val,level = queue.pop(0)
while len(nodeList) > 0:
if level == nodeList[-1][1] + 1:
node = TreeNode(int(val))
nodeList[-1][0].left = node
nodeList.append([node,level])
break
elif len(nodeList) >= 2 and level == nodeList[-2][1] + 1:
node = TreeNode(int(val))
nodeList[-2][0].right = node
nodeList.append([node, level])
break
else:
nodeList.pop()
return root
【leetcode】1028. Recover a Tree From Preorder Traversal的更多相关文章
- [LeetCode] 1028. Recover a Tree From Preorder Traversal 从先序遍历还原二叉树
We run a preorder depth first search on the rootof a binary tree. At each node in this traversal, we ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
随机推荐
- Haddop的数据计算部分原理
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import o ...
- 黑群晖DSM 6.x 配置文件grub.cfg修改 mac地址/sn等修改
新的DSM 6.x配置文件和以前的XPEnoboot的配置文件不一样了,我们可以通过OSFMount虚拟光驱软件打开img后再修改. 安装完成后运行OSFMount点击左下角-Mount new,选择 ...
- 【HANA系列】SAP HANA启动出现ERROR
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA启动出现ERRO ...
- 阅读笔记12-Java 面试题 —— 老田的蚂蚁金服面试经历
电话一面 1.自我介绍.自己做的项目和技术领域 2.项目中的监控:那个监控指标常见的哪些? 3.微服务涉及到的技术以及需要注意的问题有哪些? 4.注册中心你了解了哪些? 5.consul 的可靠性你了 ...
- 剑指offer--day02
1.1题目:用两个栈实现队列:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 1.2解题思路: 创建两个栈stack1和stack2,使用两个“先进后出”的栈实现 ...
- python每日一练:0001题
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? import o ...
- c# asp.net uploadify 上传大文件 出现的 HTTP 404 问题
用uploadify在IIS6下上传大文件没有问题,但是迁移到IIS7下面,上传大文件时,出现HTTP 404错误. 查了半天,原来是IIS7下的默认设置限制了上传大小.这个时候Web.Config中 ...
- xmake新增智能代码扫描编译模式
最近给xmake增加了一个新特性,对于一份工程源码,可以不用编写makefile,也不用编写各种make相关的工程描述文件(例如:xmake.lua,makefile.am, cmakelist.tx ...
- SQL Puzzle
1. 按条件分块取数据 有表A(CD),数据为 要求:当有CD为0的记录时,取得结果为0, 当表中没有CD=0的记录时,取得的结果为1,2,3(将CD<>0的记录都取出来) 可行SQL脚本 ...
- 尝试Vue3.0
Composition API 纯函数式 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...