思路:用一个栈来管理树的层次关系,索引代表节点的深度

方法一:

    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. 从先序遍历还原二叉树】解题报告[待完善...]的更多相关文章

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

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

  2. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  3. [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 ...

  4. [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 ...

  5. SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. Leetcode:1008. 先序遍历构造二叉树

    Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...

随机推荐

  1. org.eclipse.core.resources.bak文件导致MyEclipse每次关闭时无法保存文件

    MyEclipse关闭时提示如下信息 Problems occurred while trying to save the state of the workbench. Internal Error ...

  2. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  3. 分享知识-快乐自己:IO流基本操作

    点我参考常用API: IO图解: 相关理论: 流的概念和作用: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将 ...

  4. c语言代码风格

    简要:主要介绍了K&R风格和Allman(BSD)风格之间的缩进大小和大括号位置等区别 关于其它的代码风格,详见:Indent style - Wikipedia. 1.K&R sty ...

  5. ajax stream 一边下载二进制数据一边处理

    最近有在做 stream 下载,并且边下载 stream 边处理功能.解析二进制的功能.最初参考了 flv.js 的 flv stream 下载处理功能,发现他并没有使用的 XMLHttpReques ...

  6. 关于MFC中重载函数是否调用基类相对应函数的问题

    在重载CDialog的OnInitDialog()函数的时候,在首行会添加一句:CDialongEx::OnInitDialog();语句,这是为什么呢?什么时候添加,什么时候不添加? 实际上,我们在 ...

  7. cocos2d-x 之 CCParticleBatchNode CCParallaxNode

    //不使用 CCParticleBatchNode : 注意比较 左下角的显示信息 ; i<; ++i) { CCParticleSystem* particleSystem = CCParti ...

  8. 【LeetCode】081. Search in Rotated Sorted Array II

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  9. 非系统表空间损坏,rman备份恢复

    实验条件:有完整可用备份--查询表空间情况SQL> select tablespace_name,status from dba_tablespaces;TABLESPACE_NAME STAT ...

  10. 使用Rancher搭建K8S测试环境

    使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...