using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ public List<int> postorderTraversal (TreeNode root) { List<int> result = new List<int>(); Stack<TreeNode> st = new Stack<TreeNode>(); TreeNode cur; if(root != null)st.Push(root); while(st.Count != 0){ cur = st.Peek(); if(cur != null){ st.Pop(); st.Push(cur); st.Push(null); if(cur.right != null) st.Push(cur.right); if(cur.left != null) st.Push(cur.left); } else{ st.Pop(); result.Add(st.Peek().val); st.Pop(); } } return result; } }