(二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
Example:
Input:[1,null,2,3]
1
\
2
/
3 Output:[3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
---------------------------------------------------------------------------------------------------------------------
和leetcode 144. Binary Tree Preorder Traversal类似。
emmm,这个题是hard难度题,大概是在迭代上是不好写吧,不过递归是很容易写的。
C++代码:
/**
* 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> vec;
postorder(root,vec);
return vec;
}
void postorder(TreeNode *root,vector<int> &vec){
if(!root) return;
postorder(root->left,vec);
postorder(root->right,vec);
vec.push_back(root->val);
}
};
(二叉树 递归) leetcode 145. Binary Tree Postorder Traversal的更多相关文章
- 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(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [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: [1,nul ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 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 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- Leetcode#145 Binary Tree Postorder Traversal
原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...
随机推荐
- K3日志定时备份
K3日志超过5万条以后,每次用户登陆后,系统都会提示日志太多.但是日志又不能随意删除,所以需要做个数据库定时任务,定时把日志转移到备份表. declare @dt datetime;; SELECT ...
- AIDL使用以及原理分析
AIDL使用以及IPC原理分析(进程间通信) 概要 为了大家能够更好的理解android的进程间通信原理,以下将会从以下几个方面讲解跨进程通讯信: 1. 必要了解的概念 2. 为什么要使用aidl进程 ...
- 魅族5.0以上设备(亲测有效)激活Xposed框架的流程
对于喜欢研究手机的哥们来说,大多时候会使用到Xposed框架及种种功能强大的模块,对于5.0以下的系统版本,只要手机能获得ROOT权限,安装和激活Xposed框架是比较简单的,但随着系统版本的持续更新 ...
- Flutter项目之app升级方案
题接上篇的文章的项目,还是那个空货管理app.本篇文章用于讲解基于Flutter的app项目的升级方案. 在我接触Flutter之前,做过一个比较失败的基于DCloud的HTML5+技术的app,做过 ...
- c/c++ 模板函数的重载
模板函数的重载 普通函数可以重载,模板函数也可以重载,但规则复杂 有下面2个函数,名字相同,返回值相同就,参数不同,符合重载. template<typename T> std::stri ...
- ILRuntime官方Demo笔记
调用/执行 热更中的方法 调用热更代码中方法,写在AppDomain中,记录一下主要几个方法: AppDomain.LoadAssembly 加载热更dll 执行热更代码的方法,有两种方式: appd ...
- Win7系统下,docker构建nginx+php7环境实践
前面两章介绍的是Windows系统下如何安装和配置docker,主要原因在于,公司大多人数用的是Windows环境,想通过在Windows环境上,通过docker,构建一个公用的配置. 首先要说明的是 ...
- 前端——CSS
CSS CSS是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部导入,比较三种方式的优缺点. 语法:style = ...
- springmvc上传文件错误The current request is not a multipart request
<1>在jsp页面的<head></head>标签里面加上<meta http-equiv="Content-Type" content= ...
- php将字符串转为二进制数据串
/** * 将字符串转换成二进制 * @param type $str * @return type */ function StrToBin($str){ //1.列出每个字符 $arr = pre ...