C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal
提交网址: https://leetcode.com/problems/binary-tree-postorder-traversal/
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
分析:
AC代码:
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm> // 引入 reverse函数,对vector进行反转
using namespace std;
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> res(0);
if(root==NULL) return res;
stack<TreeNode *> st;
TreeNode *p=root;
while(!st.empty()|| p!=NULL)
{
if(p!=NULL)
{
st.push(p);
res.push_back(p->val);
p=p->right;
}
if(p==NULL)
{
p=st.top();
p=p->left;
st.pop();
}
}
reverse(res.begin(),res.end()); // 对向量及其内部结构进行反转
return res;
}
};
// 以下为测试
int main()
{
Solution sol;
vector<int> res;
TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
res=sol.postorderTraversal(root);
for(int i:res)
cout<<i<<" "; // 此处为vector遍历的方法,C++11标准支持
return 0;
}
另一解法(非递归):
后序遍历的麻烦在于访问过右子树之后,第二次访问的时候就必须访问根节点,而不是继续访问右子树,所以使用pre来记录上次访问的节点...
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
if(root == NULL) return ans;
stack<TreeNode* > s;
TreeNode * p = root;
TreeNode * pre = NULL; // 记录上次访问的节点
while(p != NULL || !s.empty()) {
while(p != NULL) {
s.push(p);
p = p->left;
}
if(!s.empty()) {
p = s.top();
s.pop();
if(p->right == NULL || p->right == pre) { // 右子树为空,或者是访问过
ans.push_back(p->val);
pre = p; // 记录刚刚访问的右节点
p = NULL;
}
else {
s.push(p);
p = p->right;
}
}
}
return ans;
}
};
C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)的更多相关文章
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
随机推荐
- C#数据同步中基本步骤和用到的相关函数
C#数据同步中基本步骤和用到的相关函数 数据同步对比步骤: 1.将两数据库中对应的数据表分别生成XML文件 /// <summary> /// 将一个DataTable以xml方式存入指定 ...
- JQUERY-自定义插件-ajax-跨域访问
正课: 1. ***自定义插件: 2. Ajax 3. *****跨域访问: 1. ***自定义插件: 前提: 已经用html,css,js实现了 2种风格: 1. jQueryUI侵入式: 1. c ...
- Python Day 7
阅读目录 内容回顾: 数据类型相互转换: 字符编码: ##内容回顾 #1.深浅拷贝 ls = [1, 'a', [10]] 值拷贝:直接赋值 ls1 = ls, ls中的任何值发生改变,ls1中的值都 ...
- 语音端点检测(Voice Activity Detection,VAD)
本文内容均翻译自这篇博文:(该博主的相关文章都比较好,感兴趣的可以自行学习) Voice Activity Detection(VAD) Tutorial 语音端点检测一般用于鉴别音频信号当中的语音出 ...
- P1081 开车旅行(Not Finish)
https://www.luogu.org/problemnew/show/P1081
- PyCharm 安装教程(Windows)
python教程 http://www.runoob.com/python3/python3-basic-syntax.html PyCharm 是一款功能强大的 Python 编辑器,具有跨平台性, ...
- 为创世纪图书馆(Library Genesis)作镜像
简介 Library Genesis的Wikipedia条目中的介绍是: Library Genesis or LibGen is a search engine for articles and b ...
- Exp3 免杀原理与实践_05齐帅
Exp3 免杀原理与实践 20154305_齐帅 想要弄懂免杀,一定得先把基础问题弄明白啊~~ 一.基础问题回答 (1)杀软是如何检测出恶意代码的? - -检测特征码: 依靠分析总结出计算机病毒中常出 ...
- Exp5 MSF基础运用 20154320 李超
实验后回答问题 用自己的话解释什么是exploit,payload,encode. exploit:起运输的作用,将数据传输到对方主机. payload:其实就是指装载的“具体内容”.就相当于shel ...
- msfconlose基本命令
命令 简介 back 从当前上下文 banner 显示显示一个令人敬畏的metasploit横幅 cd 更改当前工作目录 color 切换颜色 connect 与主机通信 edit 使用$ VISUA ...