Binary Tree Postorder Traversal Java
来源:互联网 发布:除了程序员还有能 编辑:程序博客网 时间:2024/06/06 01:36
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?
Check for coding below in detail:
/* Key to Solve: Stack1, Stack2(reverse order of stack1) PostOrder: Left -> right -> root */public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> out =new ArrayList<Integer>(); if(root==null) return out; Stack<TreeNode> stack1=new Stack<TreeNode>(); Stack<TreeNode> stack2=new Stack<TreeNode>(); stack1.push(root); while(!stack1.isEmpty()){ root=stack1.pop(); //push into stack2 for reversing stack2.push(root); //base of LIFO if(root.left!=null) stack1.push(root.left); if(root.right!=null) stack1.push(root.right); } while(!stack2.isEmpty()){ out.add(stack2.pop().val); } return out; }}
0 0
- Binary Tree Postorder Traversal Java
- 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
- 2014-6-28任务完成情况
- 红外线多点触摸技术按钮制作须知
- DAG 上的动态规划(白书)
- Effective C++ Item 32 确定你的 public 继承塑模出 is-a 关系
- 【C/C++】在代码中使用windows性能监视器 .
- Binary Tree Postorder Traversal Java
- Raw-OS源码分析之fsm(有限状态机)
- java memcache 有效期
- C++ cout输出 字符型变量的地址
- Hadoop2.0.0-CDH4中NodeManager启动不了的解决方法!
- 题目21:包含min()方法的栈
- 当我在写程序的时候,我究竟在写些什么?
- Linux下多路复用式串口操作
- 盘古越狱工具 v1.1 官方免费版