zoukankan      html  css  js  c++  java
  • LeetCode:145 二叉树的后序遍历(用栈模拟递归,记录前一个输出的节点)

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
           List<Integer> res = new LinkedList<>();
           Deque<TreeNode> stack = new LinkedList<>();
           TreeNode t = root;
           TreeNode prev = null;
           while(t!=null||!stack.isEmpty()){
               if(t!=null){
                   stack.push(t);
                   t = t.left;
               }
               else{
                   t = stack.pop();
                   if(t.right==null||t.right==prev){
                       res.add(t.val);
                       prev = t;
                       t = null;
                   }
                   else{    
                       stack.push(t);
                       t = t.right;
                   }
               }
           }
           return res;
        }
    }
  • 相关阅读:
    从错误状态恢复虚拟机
    OpenStack手动从数据库中删除实例
    对于flat_interface与public_interface的理解
    2. 拓扑图
    Cinder相关命令收集
    Nova相关命令收集
    14. Launch an instance
    9. Add the Block Storage service
    8. Add the dashboard
    7. Add a networking service
  • 原文地址:https://www.cnblogs.com/dloooooo/p/13771292.html
Copyright © 2011-2022 走看看