标签:模拟 遍历 rgb lis 二叉树 int == new col
class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new LinkedList<>(); TreeNode prev = null; TreeNode p = root; Deque<TreeNode> stack = new LinkedList<>(); while(p!=null||!stack.isEmpty()){ if(p!=null){ while(p!=null){ stack.push(p); p = p.left; } } p = stack.pop(); if(p.right==null||p.right==prev){ res.add(p.val); prev = p; p = null; } else{ stack.push(p); p = p.right; } } return res; } }
LeetCode:145 二叉树的后序遍历(用栈模拟递归,记录前一个输出的节点)
标签:模拟 遍历 rgb lis 二叉树 int == new col
原文地址:https://www.cnblogs.com/dloooooo/p/13771292.html