Leetcode 145. Binary Tree Postorder Traversal
来源:互联网 发布:centos关闭iptables 编辑:程序博客网 时间:2024/06/05 17:26
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?
第一种方法
public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); if (root == null) return res; return helper(root, res); } private List<Integer> helper(TreeNode root, List<Integer> list) { if (root == null) return list; helper(root.left, list); helper(root.right, list); list.add(root.val); return list; }
public List<Integer> postorderTraversal(TreeNode root) { LinkedList<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode p = root; while(!stack.isEmpty() || p != null) { if(p != null) { stack.push(p); result.addFirst(p.val); p = p.right; } else { TreeNode node = stack.pop(); p = node.left; } } return result; }
阅读全文
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
- ES6入门学习
- HDU --- 1878 【欧拉回路】
- webdriver+ant+jenkins自动化测试实践
- 我的easyui的正则表达式笔记
- 抛开flash,自己开发实现C++ RTMP直播流播放器
- Leetcode 145. Binary Tree Postorder Traversal
- DMA-BUF API使用指南
- implicit 详解
- AsyncTask 异步任务基本使用--下载视频
- [bzoj1877][SDOI2009]晨跑
- 亿级流量电商详情页系统的大型高并发与高可用缓存架构实战
- Camera app 分析(二)打开相机和预览过程
- UITableViewCell 消除右侧区域空白
- 去掉git diff在文件属性变化时候的比较