N叉树的前序遍历

 

 

class Solution {
public:
    vector<int>p;
    void dfs(Node* root) {
        if(root==nullptr)
            return ;
        if (root->children.size() == 0){
            p.push_back(root->val);
            return ;
        }
        for (int i = 0; i < root->children.size(); i++) {
            if(root->val!=7777777){
                p.push_back(root->val);
                root->val=7777777;
            }
            dfs(root->children[i]);
            
            
        }
    }
    vector<int> preorder(Node* root) {
        dfs(root);
        return p;
    }
};

 

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-05-26
  • 2021-12-07
  • 2021-04-24
  • 2021-07-08
  • 2021-12-23
猜你喜欢
  • 2022-02-03
  • 2021-06-17
  • 2021-05-06
  • 2021-07-30
  • 2021-06-09
  • 2021-09-09
  • 2022-01-11
相关资源
相似解决方案