代码拉取完成,页面将自动刷新
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
if (root == nullptr)
return {};
vector<int> ans;
Node *node = root;
stack<Node *> s;
s.push(root);
stack<int> sidx;
sidx.push(0);
/* 首先要在这里处理根节点 */
ans.push_back(root->val);
int cnt = 0;
while (!s.empty()) {
if (cnt < s.top()->children.size()) {
node = s.top()->children[cnt];
/* 先序遍历发生在此处 */
ans.push_back(node->val);
s.push(node);
sidx.push(cnt);
cnt = 0;
}
else {
s.pop();
cnt = sidx.top() + 1;
sidx.pop();
}
}
return ans;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。