Leetcode 145
/**
* 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;
dfs(root,res);
return res;
}
void dfs(TreeNode* root,vector<int>& res){
if(root == NULL) return;
dfs(root->left,res);
dfs(root->right,res);
res.push_back(root->val);
}
};
迭代遍历:
head表示的是上一次处理完的节点,如果处理完的节点是栈头节点的子节点,就说明可以处理根节点了。(放的时候都是根右左,根在最下面,最后才会处理根)
/**
* 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{{root}};
TreeNode* head = root;
while(!st.empty()){
TreeNode* p = st.top();
if((!p->left&&!p->right)||p->left == head||p->right == head){
res.push_back(p->val);
head = p;
st.pop();
}
else{
if(p->right) st.push(p->right);
if(p->left) st.push(p->left);
}
}
return res;
} };
Leetcode 145的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [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)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- Java实现 LeetCode 145 二叉树的后序遍历
145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...
- Java for 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 ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- Leetcode#145 Binary Tree Postorder Traversal
原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...
- LeetCode 145 二叉树的后序遍历(非递归)
题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...
随机推荐
- Seletct2
doc 博客: 基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用 <div class="span4 channelSearch&qu ...
- PTA 堆栈操作合法性(20 分)
7-1 堆栈操作合法性(20 分) 假设以S和X分别表示入栈和出栈操作.如果根据一个仅由S和X构成的序列,对一个空堆栈进行操作,相应操作均可行(如没有出现删除时栈空)且最后状态也是栈空,则称该序列是合 ...
- 每日质量NPM包拖拽文件上传_react-dropzone
一.react-dropzone 官方定义: Simple HTML5-compliant drag'n'drop zone for files built with React.js. 理解: 一个 ...
- intellij idea 破解教程
首先呼吁:抵制盗版,抵制盗版,抵制盗版 如果只是个人开发学习用,那么下面的教程可能比较适合你了 有两种方法,第一种:Activate--License server,在License server a ...
- leecode第四题(寻找两个有序数组的中位数)
题解: class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<i ...
- Debug记录(1)
今天下午在给nRF52832写程序时,莫名遇到了这个错误 错误id是一个很奇怪的数. 原代码如下: static void timers_init(void) { uint32_t timer_err ...
- js常见知识点2.面向对象相关
一.对象的概念 建议回复: 对象是一个整体,对外提供一些功能. 一切具有属性和方法的事物. 一切具有本质特征和行为的物质. 数据类型: 所有的基本数据类型都没有属性和方法. 所 ...
- top 命令常用操作
1.显示进程参数 top -c 下面操作为top命令后操作 2.按cpu/mem排序 shift + p/m 3.高亮 排序列 按x键 4.高亮 变化进程行 按b键 5.显示cpu每核的运行状态 按1 ...
- Lua和C++交互 学习记录之五:全局数组交互
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...
- 优秀的web工具网址
1.百度开源的工具 https://www.baidu.com/home/news/data/newspage?nid=3868911095318333105&n_type=0&p_f ...