【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)
【Inorder Traversal】
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
思路:
preorder用栈两三下就写完了
vector<int> preorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
tStack.push(root);
while(!tStack.empty()){
TreeNode *top = tStack.top();
nodes.push_back(top->val);
tStack.pop();
if(top->right != NULL)
tStack.push(top->right);
if(top->left != NULL)
tStack.push(top->left);
}
return nodes;
}
但是inorder不简单啊,模仿递归记录调用处然后处理完当前函数回来,下午困得不行闷头试了好几次各种bug超Memory,看了我是歌手回来发现脑子清醒了
vector<int> inorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
TreeNode * cur = root;
while(!tStack.empty() || cur != NULL){//假如处理完根的左子树了,tStack也会为空
while(cur != NULL){
tStack.push(cur);//只要当前节点有左孩子,则必须先去访问左子树,而当前节点就得入栈;
cur = cur->left;
}
cur = tStack.top();//如果当前节点为空怎么办?当然就访问它的父节点了,也就是栈顶元素;
tStack.pop();
nodes.push_back(cur->val);//访问完栈顶元素之后就需要将当前节点置为栈顶元素的右孩子
cur = cur->right;
}
return nodes;
}
Postorder与Inorder很相似,但是比Inorder复杂的地方是如何判断该节点的左右子树都已经访问过了,按照Inorder的写法左子树还是先被访问,没有问题,但是访问完左子树后不能直接访问当前节点,要判断当前节点的右子树是否已经被访问,如果没有访问则应该继续去访问右子树,最后再访问当前节点
vector<int> postorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
TreeNode *cur = root; //指向当前要检查的节点
TreeNode *previsited = NULL; //指向前一个被访问的节点
while (!tStack.empty() || cur != NULL) {
while (cur != NULL) { //只要cur有左孩子,则cur入栈,直到cur没有左孩子;
tStack.push(cur);
cur = cur->left;
}
cur = tStack.top();
if (cur->right == NULL || cur->right == previsited) {//当前节点的右孩子如果为空或者已经被访问,则访问当前节点
tStack.pop();//后序遍历访问序列中,当前节点的前驱必然是其右孩子(如果有的话))
nodes.push_back(cur->val);
previsited = cur;
cur = NULL;
}else { //否则访问右孩子,继续上述过程
cur = cur->right;
}
}
return nodes;
}
【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)的更多相关文章
- [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal
前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...
- LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...
- LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [leetcode]Binary Tree Preorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode——Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
随机推荐
- [整]常用的几种VS编程插件
通过这些编程插件,你可以方便快捷的完成编程的各项任务,以下分别作下简单介绍,欢迎讨论交流. Visual Assist(强烈推荐)网址:http://www.wholetomato.com/功能:VA ...
- 项目构建工具Gradle的使用入门(参考,只表明地址)
Gradle入门介绍:简介 http://blog.jobbole.com/71999/ Gradle入门介绍:第一个Java项目 http://blog.jobbole.com/72558/ Gra ...
- 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。
需要用到的lib包 :解析json gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三 ...
- MySql插入记录时判断
我们在开发数据库相关的逻辑过程中, 经常检查表中是否已经存在这样的一条记录, 如果存在则更新或者不做操作, 如果没有存在记录,则需要插入一条新的记录. 这样的逻辑固然可以通过两条sql语句完成. SE ...
- WebBrowers & HtmlViewers collection
WebBrowers & HtmlViewers collection 浏览: 加入我的收藏 楼主: THtmlViewerhttps://github.com/BerndGabriel/Ht ...
- 【STL】-pair的用法
初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...
- WP8 学习 在APP.XAML中加入Resources
<Application.Resources> <local:LocalizedStrings xmlns:local="clr-namespace:test1" ...
- sql alter表字段处理
--添加字段 ALTER table WCOLLECTION add CLT_ID int null default(0) --将已有字段类型为 NULL 修改为 NOT NULLalter tabl ...
- PL/SQL
function & procedure packages function --> arguments or parameters with arguments, IN, read o ...
- SharePoint表单和工作流 - Nintex篇(四)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 "Manage workflow constants" 管理工作流常量.这里可以管理工作流中 ...