# Flatten-Nested-List-Iterator

Feb 14, 2018

Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list – whose elements may also be integers or other lists.

Example 1: Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2: Given the list [1,[4,[6]]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {

}

int next() {

}

bool hasNext() {

}
};


/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
*   public:
*     // Return true if this NestedInteger holds a single integer, rather than a nested list.
*     bool isInteger() const;
*
*     // Return the single integer that this NestedInteger holds, if it holds a single integer
*     // The result is undefined if this NestedInteger holds a nested list
*     int getInteger() const;
*
*     // Return the nested list that this NestedInteger holds, if it holds a nested list
*     // The result is undefined if this NestedInteger holds a single integer
*     const vector<NestedInteger> &getList() const;
* };
*/

/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i(nestedList);
* while (i.hasNext()) cout << i.next();
*/


class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
_it = nestedList.begin(); _end_it = nestedList.end();
_tmp_it = nullptr;
}

int next() {
if (_it->isInteger()) { int ret = _it->getInteger(); ++_it; return ret; }
return _tmp_it->next();
}

bool hasNext() {
if (_it == _end_it) return false;
if (_it->isInteger()) return true;

if (_tmp_it == nullptr) {
_tmp_it = new NestedIterator(_it->getList());
}
if (_tmp_it->hasNext()) return true;
delete _tmp_it; _tmp_it = nullptr; ++_it;
return hasNext();
}
private:
vector<NestedInteger>::iterator _it, _end_it;
NestedIterator *_tmp_it;
};


dicuss中的解法会比较好一点，类别DFS来做，先用stack保存所有的元素，在调用hasNext的时候，如果栈顶是列表就将其展开并压栈（倒序），然后在递归调用hasNext，直到栈顶为数字时,然后调用next就直接返回栈顶即可：

class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
begins.push(nestedList.begin());
ends.push(nestedList.end());
}

int next() {
hasNext();
return (begins.top()++)->getInteger();
}

bool hasNext() {
while (begins.size()) {
if (begins.top() == ends.top()) {
begins.pop();
ends.pop();
} else {
auto x = begins.top();
if (x->isInteger())
return true;
begins.top()++;
begins.push(x->getList().begin());
ends.push(x->getList().end());
}
}
return false;
}

private:
stack<vector<NestedInteger>::iterator> begins, ends;
};


update at 2020-04-03

class NestedIterator {
public:
stack<vector<NestedInteger>::iterator> st;
NestedIterator(vector<NestedInteger> &nestedList) {
reversePush(nestedList);
}

void reversePush(vector<NestedInteger> &nestedList) {
auto beg = nestedList.begin();
for(int i = nestedList.size() - 1; i >= 0; --i) {
st.push(beg + i);
}
}

int next() {
auto top = st.top(); st.pop();
}

bool hasNext() {
while(!st.empty() && !st.top()->isInteger()) {
auto top = st.top(); st.pop();
reversePush(top->getList());
}
return !st.empty();
}
};

LeetCodeLeetCodeStackDesign

Verify-Preorder-Serialization-of-a-Binary-Tree

Binary-Tree-Zigzag-Level-Order-Traversal