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 ...
随机推荐
- Python——教你画朵太阳花
用python中的turtle函数画个太阳花,有以下几个步骤 1.首先,我们在开始中找到Python语言的IDLE软件脚本 2.然后出现该软件界面,如图,点击上面的Eile 3.然后在 ...
- SJCP认证题前五十题填坑
在做Java的SJCP认证试题时自己整理了一些Java基础细节知识点,以下是知识点陈列 1.标签机制:标签起作用的唯一的地方刚好在迭代语句之前(不然编译错误) continue label1 直接转到 ...
- Servlet创建完美教程
简介:Servlet其实是Server Let的缩写,是服务器端应用程序. java中有一个applet是客户端应用程序,与servlet对应.applet已经过时.Servlet作用:能在B/S架 ...
- ADC_DMA_TIM
/************************************************************************** * 文件名:ADC.h * * 编写人:离逝的风 ...
- 【python-时间戳】时间与时间戳之间的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- EF学习笔记(八):更新关联数据
学习笔记主目录链接:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 上一篇链接:EF学习笔记(七):读取关联数据 本篇原文链接:Updating Related Data 本篇主要考 ...
- PYTHON进阶(4)
学习内容: 1.html简介 2.html标签 3.堡垒机 一.html简介 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语 ...
- 自兴人工智能 python特点了解
计算机语言从语言执行分类来看,大概可分为编译型语言(如Java.c++)和解释型语言(如python.javascript) 1.编译型语言 java c++ 编写源代码.java ---> ...
- hive 使用反射函数
在hive中通过反射,调用java.net.URLDecoder,解码url 代码入下: select reflect('java.net.URLDecoder', 'decode',url, 'UT ...
- prim最小生成树
prim和DIjkstra相似,都使用了贪心策略,加一些限制条件. prim每次会找出尽量小的那个边,将其加入到树中,最终使得生成树长大. 树中有n-1个节点时或者剩下的所有边都是INF,算法结束. ...