【leetcode 968. 1028. 从先序遍历还原二叉树】解题报告[待完善...]

思路:用一个栈来管理树的层次关系,索引代表节点的深度
方法一:
TreeNode* recoverFromPreorder(string S) {
/*
由题意知,最上层节点深度为0(数字前面0条横线),而第二层节点前有1条横线,表示深度为1
树的前序遍历: 根-左-右
因此,
*/
if (S.empty()) return nullptr;
vector<TreeNode*> stack; // 结果栈
for(int i=,depth=,val=;i<S.size();)
{
for(depth=;i<S.size()&&S[i]=='-';++i) // 计算节点的深度
depth++;
for(val=;i<S.size()&&S[i]!='-';++i) // 计算数值
val=val*+S[i]-'';
while (stack.size()>depth) // 若当前栈的长度(树的高度)大于节点的深度,则可以把栈中最后几个节点pop掉(这些节点各已经成为完整的子树,可以pop掉了)
stack.pop_back();
TreeNode* node=new TreeNode(val); // 新建节点用于存放当前深度的结点
if (!stack.empty()) // 节点间关联
{
if (!stack.back()->left) stack.back()->left=node;
else if(!stack.back()->right) stack.back()->right=node;
}
stack.push_back(node);
}
return stack[];
}
【leetcode 968. 1028. 从先序遍历还原二叉树】解题报告[待完善...]的更多相关文章
- leetcode1028 从先序遍历还原二叉树 python 100%内存 一次遍历
1028. 从先序遍历还原二叉树 python 100%内存 一次遍历 题目 我们从二叉树的根节点 root 开始进行深度优先搜索. 在遍历中的每个节点处,我们输出 D 条短划线(其中 D 是 ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- [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 ...
- [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 ...
- SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode:1008. 先序遍历构造二叉树
Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...
随机推荐
- hdu 3308 线段树 区间合并+单点更新+区间查询
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Android Studio Mac版快捷键
mac上按键符号 ⌥ : option / alt ⇧ : shift ⌃ : control ⌘ : command ⎋ : esc (一)查找/查看相关 搜索任意内容 双击 sft 当前文件查找/ ...
- mybatis学习第(二)天
Mybatis第二天 高级映射 查询缓存 关于与spring的整合和反转工程我偷懒了,下次看. 使用的sql: CREATE TABLE USER( id INT PRIMARY KEY A ...
- I.MX6 USB Camera
/************************************************************************* * I.MX6 USB Camera * 说明: ...
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- Popular Cows
传送门(poj):http://poj.org/problem?id=2186 (bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051 ...
- BZOJ1636&&1699:[USACO2007JAN]Balanced Lineup
浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...
- java多线程编程核心技术——第一章总结
目录: 1.1进程.多线程的概念,及线程的优点 1.2多线程的使用 1.3currentThread()方法 1.4isAlive()方法 1.5sleep()方法 1.6getId()方法 1.7停 ...
- Amaze UI 框架
Amaze UI 框架:官方网址 http://amazeui.org/ 演示地址 http://t.amazeui.org/kitchen-sink/#/ 该UI框架提供了 桌面系统WEB框架 和 ...
- PTA实验作业-01
一.PTA实验作业 本周要求挑3道题目写设计思路.调试过程.设计思路用伪代码描述.题目选做要求: 顺序表选择一题(6-2,6-3,7-1选一题),代码必须用顺序结构抽象数据类型封装 单链表选择一题(6 ...