【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树,说明中 ...
随机推荐
- php: 0跟字符串做比较永远是true。 php大bug。
php: 0跟字符串做比较永远是true. php大bug. 如: $a = 0; if( $a == 'excel') { echo "yes"; }else{ echo &qu ...
- linux 替换目录下文件所有关键字
for i in *;do sed -ie 's/_test2/_test3/g' $i; sed -ie 's/_type2/_type3/g' $i; done 539down voteaccep ...
- Sharepoint list webpart
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js&qu ...
- codeforces 627B B. Factory Repairs(线段树)
B. Factory Repairs time limit per test 4 seconds memory limit per test 256 megabytes input standard ...
- leetcode 7 Reverse Integer(水题)
so easy,注意一下输入不爆int但是反转以后可能爆int. class Solution { public: int gao(int w){ ) ; else{ ; while(w--){ an ...
- 不要使用Android Studio的Git Commit了---->记一次debug
今天下午写了一些代码,吃晚饭时分用Android Studio commit了一下,不知道有没有选择Commit and push,结果刚才代码出bug我想回滚到上个版本的时候,发现Android S ...
- 如何在MySQl数据库中给已有的数据表添加自增ID?
由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...
- 【C/C++】scanf,printf 函数
摘自http://www.cplusplus.com 1. scanf 函数 int scanf ( const char * format, ... ); Parameters format C s ...
- JS性能之setTimeout与clearTimeout
测试环境: chrome浏览器 结论: 1 一个页面用setTimeout越多,该页面消耗的内存就会越多,几乎成正比. 2 在'startCount(1000000);-->100万'情况下,不 ...
- Hadoop问题集锦
1.Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup 使用Spark进行处理的时候 ...