Binary Tree Postorder Traversal
2015-12-18 10:28
253 查看
Binary Tree Postorder Traversal
Total Accepted: 82814 Total Submissions: 244246 Difficulty: HardGiven a binary tree, return the postorder traversal of its nodes' values.
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?
(M) Binary Tree Inorder Traversal
/** * 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; stack<pair<TreeNode*,bool>> paths; TreeNode* p = root; while(p || !paths.empty()){ while(p){ paths.push(make_pair(p,false)); p = p->left; } while(!paths.empty()){ pair<TreeNode*,bool>& node = paths.top(); if(node.second){ res.push_back(node.first->val); paths.pop(); }else{ p = node.first->right; node.second = true; break; } } } return res; } };
Next challenges: (H) Binary Tree Maximum Path Sum (E) Implement Stack using Queues (M) Binary Tree Longest Consecutive Sequence
相关文章推荐
- TypeScript笔记[5]泛型+Dictionary 转
- 【Java EE 学习 74 上】【数据采集系统第六天】【使用Jfreechart的统计图实现】【Jfreechart的基本使用方法】
- 从10w条记录随机取出n条记录高效率解决方案
- Android慎用layout嵌套, 尽量控制在5层以下java.lang.StackOverflowError
- 如何准确的向工程师传达动效设计?
- Ext.js5的列冻结(5)
- html修炼养成--怎样使用Swiper
- solrcloud搭建
- eclipse安装svn插件
- js中的坑
- 构建基于Jenkins+maven+git+tomcat7的持续集成环境
- 蓝牙HID协议介绍
- 第六章TCP和UDP基本原理(续)
- 【跟着大磨马学IT编程(安卓java程序员)】第十八天 课程内容概述
- python学习记录(2)高级特性
- VS2013 VC++ 编译遇到不支持非unicode字符集的错误
- win7, 64位 python2.7 安装pygame(转载)
- zabbix监控MySQL
- mysql批量插入
- Direct Path Insert - APPEND HINT and TM Enqueue LOCK Behaviour (文档 ID 1317447.1)