将Deque定义为LinkedList

时间:2018-05-21 16:51:08

标签: java linked-list tree stack deque

我正在使用迭代方法检查代码预订二进制树。看起来他们这样做:

Deque< TreeNode> stack = new LinkedList< TreeNode>();

为什么不这样做:

Deque< TreeNode> stack = new Deque< TreeNode>();

我没有看到他们在代码中使用了特别是来自LinkedList的东西。

public class PreOrder {  
  public static List<Integer> preorderTraversalIterative(TreeNode root) {
    List<Integer> preorder = new ArrayList<Integer>();
    if (root == null) {
      return preorder;
    }
    Deque<TreeNode> stack = new LinkedList<TreeNode>(); //Why?
    stack.offerFirst(root);
    while(!stack.isEmpty()) {
      TreeNode cur = stack.pollFirst();
      if (cur.right != null) {
        stack.offerFirst(cur.right);
      }
      if (cur.left != null) {
        stack.offerFirst(cur.left);
      }
      preorder.add(cur.key);
    }
    return preorder;
  }
} 

1 个答案:

答案 0 :(得分:2)

abstract是一个界面;如果没有为LinkedList方法的所有提供实现,就无法实例化它,groupBy已经是objectsObservable .groupBy(object -> object) .flatMapMaybe(sameObjectsObservable -> { Object object = sameObjectsObservable.getKey(); return sameObjectsObservable .count() .filter(shouldFilter) .ignoreElement() .andThen(Maybe.just(object)); })