145. Binary Tree Postorder Traversal
2015-09-21 02:45
399 查看
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> results = new ArrayList<Integer>(); if(root == null) return results; Deque<TreeNode> stack = new ArrayDeque<TreeNode>(); TreeNode walker = root; TreeNode lastProcessed = null; while(walker!=null || !stack.isEmpty()) { if(walker == null) { TreeNode processing = stack.peek(); if(processing.right != null && processing.right != lastProcessed) { walker = processing.right; } else { results.add(processing.val); lastProcessed = stack.pop(); } } else { stack.push(walker); walker = walker.left; } } return results; } }
相关文章推荐
- abstract class和interface的区别
- win8 win10 安装msi 提示2502、2503的错误代码
- Unique Binary Search Trees II 解答
- Perl递归遍历指定文件下的文件
- 虚拟地址、线性地址和物理地址的转换
- MyBatis学习笔记(七)——Mybatis缓存
- .plist 的读写
- 安装grunt
- Java实现堆排序
- Linux操作系统内核对RTC的编程详解 (5)
- Perl语言入门笔记 第十七章 高级perl技巧(eval, grep, map)
- MyBatis学习笔记(六)——调用存储过程
- Perl语言入门笔记 第十六章 进程管理
- MyBatis学习笔记(五)——实现关联表查询
- Perl语言入门笔记 第十五章 智能匹配与given-when结构
- MyBatis学习笔记(四)——解决字段名与实体类属性名不相同的冲突
- CSS3径向渐变linear-gradient
- Linux操作系统内核对RTC的编程详解 (4)
- Perl语言入门笔记 第十四章 字符串与排序
- MyBatis学习笔记(三)——优化MyBatis配置文件中的配置