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;
    }
};

 

posted @ 2020-11-08 20:37  知道了呀~  阅读(240)  评论(0编辑  收藏  举报