前往二叉树的:前序,中序,后序 遍历算法

方法一:递归

    vector<int> res;
vector<int> postorderTraversal(TreeNode* root) {
if (!root) return res;
if (root->left) postorderTraversal(root->left);
if (root->right) postorderTraversal(root->right);
res.push_back(root->val);
return res;
}

方法二:非递归

    vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode*> S;
TreeNode* p=root, *r=nullptr;
while (p||!S.empty())
{
if (p)
{
S.push(p);
p=p->left;
}
else
{
p=S.top();
if (p->right&&p->right!=r)
p=p->right;
else
{
S.pop();
res.push_back(p->val);
r=p;
p=nullptr;
}
}
}
return res;
}

方法三:非递归

    vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode*> S;
TreeNode* p=root;
S.push(p);
while (!S.empty())
{
p=S.top();
S.pop();
if (p->left) S.push(p->left);
if (p->right) S.push(p->right);
res.insert(res.begin(),p->val);
}
return res;
}

【leetcode 145. 二叉树的后序遍历】解题报告的更多相关文章

  1. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  2. Java实现 LeetCode 145 二叉树的后序遍历

    145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...

  3. LeetCode 145 二叉树的后序遍历(非递归)

    题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...

  5. Leetcode 145. 二叉树的后序遍历

    题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...

  6. LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)

    题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [ ...

  7. LeetCode 145 ——二叉树的后序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 递归得到 ...

  8. LeetCode:二叉树的后序遍历【145】

    LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  9. 【LeetCode】145. 二叉树的后序遍历

    145. 二叉树的后序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 后序 遍历. 示例 输入: [1,null,2,3] 1 \ 2 / 3 输 ...

随机推荐

  1. Visual C++中error spawning cl.exe解决方法

    点击“Tools”,选择“选项”,选择“选项”中的“目录” 目录的目录下有四个选项 需要对它们设置正确的路径 我是按照默认路径安装的 可执行文件: C:\Program Files\Microsoft ...

  2. python's eleventh day for me

    python2 中没有nonlocal. 函数名是什么? 函数名就是函数的名字, 本质:变量,特殊的变量. 1.单独打印函数名: def func(): print(666) print(func) ...

  3. Nodejs开发Office插件

    如果使用Nodejs开发Office插件,需要借助Yeoman包去生成Office插件 yeoman地址是:http://yeoman.io/generators/,输入office 源码地址是:ht ...

  4. leetcode888

    class Solution { public: int Binary_Search(vector<int> x, int N, int keyword) { , high = N - , ...

  5. python发送包含html、图片、附件和链接的邮件

    1.smtplib模块的使用 smtplib库用来发送邮件.需要用到的函数如下: 连接到SMTP服务器,参数为SMTP主机和端口: SMTP.connect([host[,port]]) 登录SMTP ...

  6. Spring Cloud Feign 1(声明式服务调用Feign 简介)

    Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...

  7. Lucene Payload 的研究与应用

    Payload (元数据) 诞生于 Lucene 的2.2 版本,它是在 Lucene 2.1 索引文件格式的基础上扩展而来,提供了一种可以灵活配置的高级索引技术,在某些特定应用场景下能优化基于 Lu ...

  8. 刷题向》关于一道奇怪的贪心(田忌赛马)BZOJ1034(NORMAL-)

    这道题一看就是一道贪心,比较简单,但是越容易的题考试的时候越容易错... 没什么好说的,一遍SORT之后,直接强行田忌赛艇(滑稽脸)就好啦. 注意在对比大小的时候不仅要从前还要从后同时,不过这两个情况 ...

  9. VMware设置及linux静态ip设置

    1.   VMWARE虚拟机NAT模式上网设置 1.1. VM虚拟机设置 1.1.1.   虚拟机全局设置 启动虚拟机选择[虚拟网络编辑器] 如果需要管理员权限点[更改设置],没有提示这忽略这一步 选 ...

  10. mysql中如何不重复插入满足某些条件的重复的记录的问题

    最近在项目中遇到了这样的一个问题“: 在mysql数据库中需要每次插入的时候不能插入三个字段都相同的记录.在这里使用到了 insert into if not exists  和insert igno ...