public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { if (pRoot == null){ return new ArrayList<>(); } ArrayList<ArrayList<Integer>> list = new ArrayList<>(); // 从左往右 Stack<TreeNode> first = new Stack<>(); // 从右往左 Stack<TreeNode> second = new Stack<>(); first.push(pRoot); int flag = 1; while (!first.isEmpty() || !second.isEmpty()){ ArrayList<Integer> curList = new ArrayList<>(); if (flag % 2 != 0){ while (!first.isEmpty()){ TreeNode curNode = first.pop(); curList.add(curNode.val); if (curNode.left != null){ second.push(curNode.left); } if (curNode.right != null){ second.push(curNode.right); } } }else { while (!second.isEmpty()){ TreeNode curNode = second.pop(); curList.add(curNode.val); if (curNode.right != null){ first.push(curNode.right); } if (curNode.left != null){ first.push(curNode.left); } } } flag++; list.add(curList); } return list; }