import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); if(root == null) return res; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode pre = null; stack.push(root); while(!stack.isEmpty()){ TreeNode curr = stack.peek(); if((curr.left == null && curr.right == null) || (pre != null && (pre == curr.left || pre == curr.right))){ res.add(curr.val); pre = curr; stack.pop(); }else{ if(curr.right != null) stack.push(curr.right); if(curr.left != null) stack.push(curr.left); } } return res; } }
后序遍历,左右根,左右节点都为空,添加根元素,当前节点不完空,不是左右节点,添加元素
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦