[Lintcode] 68. Binary Tree Postorder Traversal
来源:互联网 发布:伤不起dj网络歌手 编辑:程序博客网 时间:2024/06/06 14:13
给出一棵二叉树,返回其节点值的后序遍历。
public class Solution { /** * @param root: The root of binary tree. * @return: Postorder in ArrayList which contains node values. */ public List<Integer> postorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode prev = null; TreeNode curr = root; if (root == null) { return result; } stack.push(root); while (!stack.empty()) { curr = stack.peek(); if (prev == null || prev.left == curr || prev.right == curr) { if (curr.left != null) { stack.push(curr.left); } else if (curr.right != null) { stack.push(curr.right); } } else if (curr.left == prev) { if (curr.right != null) { stack.push(curr.right); } } else { result.add(curr.val); stack.pop(); } prev = curr; } return result; }}
阅读全文
0 0
- [Lintcode] 68. Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- 总结-2017/9/4
- Python爬虫实战(一):爬取豆瓣电影top250排名
- c# 插入排序
- 文章标题
- SpringMVC中拦截/和拦截/*的区别
- [Lintcode] 68. Binary Tree Postorder Traversal
- Maven2部署构件到Nexus时出现的Failed to transfer file错误
- MySQL标准变量、临时变量、系统变量
- QT程序打包--教你做一个可安装的应用程序
- STS 下 ctrl + shift + o 重新导包快捷键失效解决方法
- 在Ubuntu系统基于ROS使用废旧Android手机摄像头搭建监控设备
- Spark Structured Streaming框架(2)之数据输入源详解
- Ubuntu16.04下安装FFmpeg(超简单版)
- 安卓handler初识(三)