LeetCode Binary Tree Postorder Traversal(数据结构)
题意:
用迭代法输出一棵二叉树的后序遍历结果。
思路:
(1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
if(root==NULL) return ans; stack<TreeNode*> stac1; stac1.push(root);
stack<int> stac2; stac2.push(-);//表示哪个孩子已经被遍历,-1表示其孩子未被遍历 while(!stac2.empty())
{
TreeNode *top=stac1.top();
int &dir=stac2.top();
if(dir<)
{
if(dir==- && top->left)
{
stac1.push(top->left);
dir=;
stac2.push(-);
}
else if(top->right)
{
stac1.push(top->right);
dir=;
stac2.push(-);
}
else dir=;
}
else
{
ans.push_back(top->val);
stac2.pop();
stac1.pop();
}
}
}
};
AC代码
(2)用一个栈,模拟逆过程,先输出根,再输出右子树,再输出左子树,最后将输出结果反置即可。这样的好处就是不需要记录根节点了,这和层次遍历的过程差不多。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
if(root==NULL) return ans; stack<TreeNode*> stac;
stac.push(root); while(!stac.empty())
{
TreeNode *top=stac.top();
ans.push_back(top->val);
stac.pop();
if(top->left) stac.push(top->left);
if(top->right) stac.push(top->right);
}
reverse(ans.begin(),ans.end());
}
};
AC代码
(3)O(1)的空间,依然O(n)的复杂度。待写。。。。
LeetCode Binary Tree Postorder Traversal(数据结构)的更多相关文章
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- Sql Server 主从数据库配置
网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...
- 搜索功能demo
代码如下: <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edg ...
- 各种常用函数 (SQL)
数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001 ...
- synergy配置 Ubuntu作Server, Win 7作client
Synergy 允许你轻松地在你办公桌上多台计算机之间共享你的鼠标和键盘,它免费并且开放源代码.你只要将鼠标(指针)从一台计算机的屏幕边缘移出到另一个屏幕就行 了.甚至可以共享你的剪贴板.你所需要的仅 ...
- not use jquery
document.getElementById('myElement');document.querySelector('#myElement'); document.getElementsByCla ...
- 精通JS 笔记
一,javascript数据类型:undefined,null,boolean,number,string,object 五种加一种复杂类型. 注意大小写,区分大不写函数:functiontypeof ...
- net IL的一些探索
查看IL有2个工具比较好用,一个是大名鼎鼎的Reflector,但已经转向收费,另一个是开源的ILSpy,强大好用,对linq和lambda表达示的支持很好.相关的书籍也很多,比如这本Microsof ...
- STL-算法
#include <algorithm> 1. max_element(v.begin(), v.end()); 注意,所有的区间全部是半开区间,如果数组包含20-40,通过find找出2 ...
- string.format
string.Format("{0:#,0}", c.num), //千分号,有小数就保留2位小数 string.Format("{0:N2}", c.amou ...
- zoj 3228 覆盖及非覆盖串的多次匹配
题目题意: 给定多个小串,在一个长串中寻找这些串的匹配次数,有些统计的是可覆盖的,有些统计的是非覆盖的 先可以简单理解一下,建立ac自动机后,当前节点包含的字符串必然被把它作为fail指针的节点包含, ...