双栈方法实现 二叉树的后序的,非递归的,遍历算法

原文链接: https://www.jianshu.com/p/fff56a761dde

来源

https://www.jianshu.com/p/fff56a761dde

/**
    * 后序遍历 非递归
    * 双栈法
    * @param root
    */
   public static void postOrder2(Node root) {
       Stack stack = new Stack();
       Stack output = new Stack();
       Node node = root;
       while (node != null || !stack.isEmpty()) {
           if (node != null) {
               stack.push(node);
               output.push(node);
               node = node.rightNode;
           } else {
               node = stack.pop();
               node = node.leftNode;
           }
       }

       while (output.size() > 0) {
           Node n = output.pop();
           System.out.print(n.data + "\t");
       }
   }
  


你可能感兴趣的:(算法)