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 ...
随机推荐
- 在mybatis中resultMap与resultType的区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMa ...
- Lintcode27-Reverse 3-digit Integer
Reverse a 3-digit integer. Example Example 1: Input: number = 123 Output: 321 Example 2: Input: numb ...
- java根据URL获取HTML内容
之前我写脚本,是想获取HTML内容的. 但是呢...一方面编码困扰着我,于是我写了这个: java根据URL获取网页编码 然后呢,每个网站是不是GZIP还得判断,贼麻烦... 但是没办法啊,麻烦也得写 ...
- R的极客理想系列文章--转载
http://blog.fens.me/series-r/ R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在 ...
- 转一个集成速锐的ss 回头试试 补充加速一、Vultr安装锐速
https://liyuans.com/archives/ssr-serverspeeder-onekey.html Debian/Ubuntu 系统 ShadowsocksR 一键安装脚本 (集成锐 ...
- ImgNoGoodWindow
using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...
- Video 对象方法 canPlayType()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- linux df
显示磁盘使用情况 [hadoopuser@CNSZ443239 ~]$ df 文件系统 1K-块 已用 可用 已 ...
- Java Object类及其equals方法
基本概念: Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入: Object类是所有Java类的祖先.每个类都使用 Object 作为超 ...
- java常用技术名词解析
1.1 token Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便 将此Token返回给客户端,以后客户端只需带上这个Token前来请求数 ...