[LeetCode] 145. Binary Tree Postorder Traversal
来源:互联网 发布:天刀离玉堂捏脸数据 编辑:程序博客网 时间:2024/05/16 01:16
思路:
第一个还是简单的递归版本. 第二个是iterative版本. 第二个就是做一个反向preorder遍历, 然后再逆转结果表即可.
void helper(vector<int>& res, TreeNode* root) { if (! root) return; helper(res, root->left); helper(res, root->right); res.push_back(root->val);}vector<int> postorderTraversal(TreeNode* root) { vector<int> res; helper(res, root); return res;}
vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if (! root) return res; stack<TreeNode*> stk; stk.push(root); while (! stk.empty()) { TreeNode* cur = stk.top(); stk.pop(); res.push_back(root->val); if (root->left) stk.push(root->left); if (root->right) stk.push(root->right); } reverse(res.begin(), res.end()); return res;}
0 0
- [LeetCode]145.Binary Tree Postorder Traversal
- [leetcode] 145.Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal LeetCode
- Leetcode 145. Binary Tree Postorder Traversal
- [LeetCode]145. Binary Tree Postorder Traversal
- Leetcode 145. Binary Tree Postorder Traversal
- leetcode 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode-145.Binary Tree Postorder Traversal
- [leetcode] 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- leetcode 145. Binary Tree Postorder Traversal
- [LeetCode]problem 145. Binary Tree Postorder Traversal
- Leetcode 145. Binary Tree Postorder Traversal
- [LeetCode] 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode:145. Binary Tree Postorder Traversal
- APP 状态
- 统计文本关键词频数
- 算法 Tricks(四)—— 判断序列中的字符/数值是否交替出现
- URI 和URL
- Parameter 'xxx' not found. Available parameters are [3, 2, 1, 0, param1, param2, param3, para
- [LeetCode] 145. Binary Tree Postorder Traversal
- 获取两个字符串之间最长公共子串的长度
- IP与国家对应关系
- 归并排序时间复杂度----主定理
- webgoat Easy Run ( For non-developers )
- python操作XML
- eclips导入GitHub的java文件
- Android 更新ProgressBar
- 使用jatoolsPrinter最新免费版实现套打