leetcode - Binary Tree Postorder Traversal
2016-01-03 21:24
525 查看
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
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?
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //BinTree的后序遍历 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x),left(NULL),right(NULL) {} }; class Solution { public: std::vector<int> postorderTraversal(TreeNode *root) { std::vector<int> vec; BinTree(root,vec); return vec; } void BinTree(TreeNode *root,std::vector<int> &vec) { if(root != NULL) { BinTree(root->left,vec); BinTree(root->right,vec); vec.push_back(root->val); } } private: std::vector<int> vec; };
相关文章推荐
- 图片的拉伸问题,修改frame的方式,
- OK6410用DNW下载led和ds18b20的裸机程序
- 深入浅出Java类和对象的初始化
- UICollectionView: 1>UICollectionViewFlowLayout流式布局
- Linux命令之修改主机名
- 流体布局CSS3中 calc()的使用 顺带box-sizing
- Maven项目显示红叹号
- Best Time to Buy and Sell Stock with Cooldown
- Mac下Jenkins安装(一)
- mysql 服务启动报1607 error
- 活动组件(一)
- 工厂三兄弟之工厂方法模式(三):日志记录器的工厂方法模式解决方案
- 请关闭你的等死模式
- 抽象工厂模式
- 构造函数的使用
- RESTful_基础知识
- Linux下如果你的网络设定妥当了,但是却老是发现网络不通,应该如何进行测试?
- 安卓中测量控件的高度
- iOS 疑难杂症(什么鬼系列)— — UITableView 添加 tableFooterView 旋转屏幕后收不到点击事件!!!
- BZOJ 刷题记录 PART 6