标签:
http://bookshadow.com/weblog/2015/01/19/leetcode-binary-tree-postorder-traversal/
http://bookshadow.com/weblog/2015/01/19/binary-tree-post-order-traversal/
记不住啊总,不如试试双栈法,借用如上链接提示,反向preorder
要注意压入栈的时候子节点左右顺序
public class Solution { public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> res = new ArrayList<Integer>(); if(root==null) return res; Stack<TreeNode> st = new Stack<TreeNode>(); Stack<TreeNode> out= new Stack<TreeNode>(); st.push(root); while(!st.isEmpty()){ TreeNode n= st.pop(); out.push(n); if(n.left!=null){ st.push(n.left); } if(n.right!=null){ st.push(n.right); } } while(!out.isEmpty()){ TreeNode o = out.pop(); res.add(o.val); } return res; } }
Binary Tree Postorder Traversal
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4573232.html