145. Binary Tree Postorder Traversal
来源:互联网 发布:灵异知乎 编辑:程序博客网 时间:2024/05/17 02:04
1、题目描述
二叉树的后序遍历。
2、思路
后序遍历顺序为左右中。反过来也就是中右左。可以借助先序遍历的思想,先中右左遍历,再把答案逆序。
3、代码
vector<int> postorderTraversal(TreeNode* root) { vector<int> ans; if(root==NULL) return ans; stack<TreeNode*>s; while(root){ ans.push_back(root->val); if(root->left) s.push(root->left); root=root->right; if(root==NULL&&!s.empty()){ root = s.top(); s.pop(); } } reverse(ans.begin(),ans.end()); return ans; }
阅读全文
0 0
- 145.Binary Tree Postorder Traversal
- 145.Binary Tree Postorder Traversal
- 145.Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- yum安装rsync
- 怎么在Mac上修复/格式化磁盘?
- linux:vim setting of paste
- Linux : select()详解 和 实现原理【转】
- 干货 | 机器学习基石02 Learning to Answer Yes/No
- 145. Binary Tree Postorder Traversal
- 新站seo优化的核心步骤你没了解过
- prototype
- MySQL数据库的更新和删除(丛林战争项目)
- electron+vue
- [游戏数据表]七日杀 7DAYS TO DIE全物品合成表
- 如何在本地搭建FTP服务器细节全过程
- 洛谷2244选举预测
- DrawerLayout + tablayout