0
点赞
收藏
分享

微信扫一扫

leetcode 589. N 叉树的前序遍历【非递归】

承蒙不弃 2023-02-22 阅读 53


#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 叉树的前序遍历【非递归】_#define


举报

相关推荐

0 条评论