#define debug(x) cout<<#x<<": "<<(x)<<endl;
class Solution {
public:
vector<int> preorder(Node* r) {
stack<Node*>st;
vector<int>ret;
if(r != nullptr){
st.push(r);
}
while(!st.empty()){
auto t = st.top();
st.pop();
//debug(t->val)
ret.push_back(t->val);
for(auto it = t->children.rbegin();it != t->children.rend();it++){
st.push(*it);
}
}
return ret;
}
};
leetcode 589. N 叉树的前序遍历【非递归】
最新推荐文章于 2022-03-10 12:27:41 发布