(二叉树 递归) 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
原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...
随机推荐
- Vue与React两个框架的区别对比
简单介绍 React--Facebook创建的JavaScript UI框架.它支撑着包括Instagram在内的大多数Facebook网站.React与当时流行的jQuery,Backbone.js ...
- Unity 2D 效应器与来回移动的实现
1.效应器 Point Effector 2D: 点效应器.进入区域,吸引或排斥物体 Area Effector 2D: 区域效应器,可以用来做马里奥的管道移动效果 Surface Effector ...
- SQL Server 事务日志文件已满,收缩日志文件(9002)
错误如下图: 1.数据库 → 属性 → 选项 → 恢复模式 → 选择‘简单’:如下图: 2.任务 → 收缩 → 文件类型‘文件’ → 收缩模式‘在释放未使用的空间前重新组织页’,将文件收缩到K,大小填 ...
- 一天一个Linux命令--nmcli
nmcli命令连接和删除SSID nmcli d wifi ...
- QPen
Help on class QPen in module PyQt5.QtGui: class QPen(sip.simplewrapper) | QPen() | QPen(Qt.PenStyl ...
- ABP之session
ABP提供了一个IAbpSession接口,可以在不使用ASPNET的session的情况下获取当前用户和租户.IAbpSession还被ABP中的其他结构(如设置和授权系统)完全集成和使用. 注入s ...
- Net包管理NuGet(1)nuget的使用方法
关于nuget,有很多介绍想要深入了解的可以看看官网https://docs.microsoft.com/zh-cn/nuget/what-is-nuget 本文简单介绍让不知道的可以快速了解 1,使 ...
- [转帖]Windows Server 2016各种版本介绍
Windows Server 2016各种版本介绍 http://www.5sharing.com/js/zx/872.html windows server的版本 时间:2018-10-06 10: ...
- CodeForces 1151E Number of Components
题目链接:http://codeforces.com/problemset/problem/1151/E 题目大意: n个人排成一个序列,标号为 1~n,第 i 个人的学习成绩为 ai,现在要选出学习 ...
- IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository
转自:IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository 今天将IntellIJ IDEA 关于Maven的配置总结一下,方便以后可参考. IDEA版本: Intel ...