LeetCode题解:Binary Tree Postorder Traversal
2013-11-10 05:10
323 查看
Binary Tree Postorder Traversal
Given 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?
思路:
无难度。
题解:
/** * Definition for binary tree * 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> ret; if (root == nullptr) return ret; list<pair<TreeNode*, bool>> traverse; traverse.push_back(make_pair(root, false)); while(!traverse.empty()) { auto& front = traverse.front(); if (front.second) { // second time visit it ret.push_back(front.first->val); traverse.pop_front(); } else { // first time visit it front.second = true; if (front.first->right) traverse.push_front(make_pair(front.first->right, false)); if (front.first->left) traverse.push_front(make_pair(front.first->left, false)); } } return ret; } };
相关文章推荐
- Longest Consecutive Sequence 从无序数组中找最长的连续数字 @LeetCode
- socket相关知识
- 安卓升级提示 phoneGap APK软件更新提示
- python调用dll
- Remove Duplicates from Sorted List II 链表移除重复值@LeetCode
- cocos2d-x-2.2/samples/Cpp/SimpleGame/ 问题
- 搭建Titanium开发环境
- c++ 运算符的重载
- VMware Appliance的默认用户名和密码
- python常用
- 1110
- Excel poi导出
- C++ tips .
- redis的sort排序
- ThinkPHP调用存储过程不能返回结果集的解决方案
- Java之BerkeleyDB(一)
- 黑马程序员---函数与数组总结
- 最烂的程序员就是不专业。。
- 表单元素的submit()方法和onsubmit事件
- 6-3-2 二叉树层次遍历(BFS)