【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树,说明中 ...
随机推荐
- ThinkPad.E440_安装固态硬盘
1.ThinkPad(E440) 加装SSD固态硬盘,并改装双硬盘_百度经验.html(https://jingyan.baidu.com/article/9f63fb91856ec7c8400f0e ...
- $.ajax()方法详解(转)
以下内容转自:http://www.cnblogs.com/tylerdonet/p/3520862.html 尊重原创,请访问原创文章 jquery中的ajax方法参数总是记不住,这里记录一下. ...
- [转]Eclipse快捷键_01_常用快捷键汇总
(注:红色标出来的是经常使用到的快捷键,磨刀不误砍柴工啊...) 一.常用快捷键 Shift+Alt+L: 自动补全等号左边对象 .(用鼠标选中这一行,然后按组合键Shift+Alt+L,在弹出的对 ...
- I.MX6 Python3 OpenCV
/************************************************************************* * I.MX6 Python3 OpenCV * ...
- 解决编译warning:warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]
问题: 环境:ubuntu 12.04,g++版本4.6.3,编译目标文件时出现warnings: u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/s ...
- 股神小D
题目大意: 给定一棵树,每一条边有$L,R$两种权值,求有多少条路径满足$\max(L)\leq\min(R)$. 解法$1-$点分治$+$二维数点 统计树上的路径应首先想到点分治,我们很显然可以搜出 ...
- js之__proto__原型链
可参考: http://blog.csdn.net/irelandken/article/details/7297490
- jquery--find与children方法的区别
children方法: find方法: 通过以上的解释,可以总结如下: 1:children及find方法都用是用来获得element的子elements的,两者都不会返回 text node,就 ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- GPRS模块用TCP实现MQTT协议(基于SIM900A)
mqtt部分: int strlen(char *str) { int len = 0; while (*str != '\0') { len++; str++; } return len; } // ...