68-Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal My Submissions QuestionEditorial Solution
Total Accepted: 97358 Total Submissions: 273744 Difficulty: Hard
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?
二叉树的后续遍历,不使用递归实现
思路:类似于中序,需要额外使用一个map来保存相应的访问转态
/**
* 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> res;
if(root==NULL)return res;
stack<TreeNode*> st;
st.push(root);
TreeNode * rn=root;
map<TreeNode*,int> map;
map[rn]=1;
while(!st.empty()){
while(rn!=NULL&&rn->left!=NULL&&!map.count(rn->left)){//走到最左下角
st.push(rn->left);
rn = rn->left;
map[rn]=1;
}
if(rn->right!=NULL&&!map.count(rn->right)){ //右子树不空进入右子树
rn = rn->right;
st.push(rn);
map[rn]=1;
}
else { //为空则访问该节点
res.push_back(rn->val);
st.pop();
if(!st.empty())rn = st.top(); //回溯到上一个节点
}
}
return res;
}
};
68-Binary Tree Postorder Traversal的更多相关文章
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- 阿里Nacos部署
Nacos的部署 一.单机部署 **4.修改 Nacos 存储为 Mysql** 二.集群部署 1.机器部署列表 2.修改 `nacos/conf/application.properties`中的端 ...
- FastAPI 学习之路(五十六)将token存放在redis
在之前的文章中,FastAPI 学习之路(二十九)使用(哈希)密码和 JWT Bearer 令牌的 OAuth2,FastAPI 学习之路(二十八)使用密码和 Bearer 的简单 OAuth2,Fa ...
- 在浏览器上开发GO和Vue!(基于code-server)
在浏览器上开发GO和Vue!(基于code-server) 曾几何时,开发者们都被安装编程环境苦恼,尽管现在很多语言的开发环境已经不难装了,但是如果我们能有一个运行在云端的编译器,那么我们就可以随时随 ...
- Python课程笔记 (五)
今天主要学习图形用户界面,更多的还是要我们自己去实际操作,课仿佛上了一半就完了,分享一下课程(这里在SixthClass)的源码: https://gitee.com/wang_ming_er/pyt ...
- SQLServer聚集索引导致的插入性能低
1,新表默认会在主键上建立聚集索引.对于非专业DBA, 默认配置在大多数情况下够用. 2,当初为了优化查询速度. 把聚集索引建立在非自增主键的唯一索引列. 数据量上千万后,插入性能开始显现不足. 随着 ...
- Tcpdump 常用命令、参数记录
一.介绍 一个关于Centos Tcpdump 的个人工作总结. 二.参数介绍: 1. -i: 指定要进行抓包的网卡 2.-s0 :表示每个报文的大小是接收到的指定大小,如果没有这个选项,则超过 ...
- 盘点 GitHub 年度盛会|附视频
「Universe 2021」是 GitHub 于今年举办的开发者盛会,本次 Universe 2021 大会采用线上直播模式,为期两天已于上周落下帷幕. 这是 GitHub 举办的一年一度开发者盛会 ...
- Go语言核心36讲(Go语言进阶技术十四)--学习笔记
20 | 错误处理 (下) 在上一篇文章中,我们主要讨论的是从使用者的角度看"怎样处理好错误值".那么,接下来我们需要关注的,就是站在建造者的角度,去关心"怎样才能给予使 ...
- IP数据报中如果不分片,分片标志值是什么?
过了好久才解决这个简单的问题,罪过罪过- 答案:如果IP数据报不分片,分片标志DF(Don't Fragment)会被设置为1.分片标志MF(More Fragment)设置为0. 下面是详细解释: ...
- electron获取不到remote
electron获取不到remote 问题 // 渲染进程 let remote = require('electron').remote console.log(remote) // undefin ...