public List<List<Integer>> levelOrderBottom(TreeNode root) {

        if (root == null)
            return new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        Stack<ArrayList<Integer>> finalStack = new Stack<>();

        queue.add(root);
        int last = 1;
        int current = 0;

        ArrayList<Integer> l = new ArrayList<>();

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            l.add(node.val);
            last = last - 1;
            if (node.left != null) {
                queue.add(node.left);
                current = current + 1;
            }
            if (node.right != null) {
                queue.add(node.right);
                current = current + 1;
            }

            if (last == 0) {
                last = current;
                current = 0;
                finalStack.push(l);
                l = new ArrayList<>();
            }
        }

        List<List<Integer>> ret = new ArrayList<>();
        while (!finalStack.isEmpty()) {
            ArrayList<Integer> a = finalStack.pop();
            ret.add(a);
        }

        return ret;
    }