3.3 树和森林的遍历

3.3 树和森林的遍历_哔哩哔哩_bilibili

 

// 树的先根遍历
// 若树非空,先访问根结点,再依次对每棵子树进行先根遍历;(与对应二叉树的先序遍历序列相同)
// 树的先根遍历序列与这棵树相应二叉树的先序序列相同
void PreOrder(TreeNode *R){
   if(R!=NULL){
      visit(R); //访问根节点
      while(R还有下一个子树T)
         PreOrder(T); //先根遍历下一个子树
   }
}

 

// 树的后根遍历
// 若树非空,先依次对每棵子树进行后根遍历,最后再访问根结点。(深度优先遍历)
// 树的后根遍历序列与这棵树相应二叉树的中序序列相同
void PostOrder(TreeNode *R){
   if(R!=NULL){
      while(R还有下一个子树T)
         PostOrder(T); //后根遍历下一个子树
      visit(R);        //访问根节点
   }
}

 

589. N 叉树的前序遍历 - 力扣(LeetCode)

// C
void PreOrder(const struct Node* root, int* res, int* pos) {
    if(!root) return;
    res[(*pos)++] = root->val;
    for(int i = 0; i < root->numChildren; i++) {
        PreOrder(root->children[i], res, pos);
    }
}
int* preorder(struct Node* root, int* returnSize) {
    int *res = (int*)malloc(sizeof(int)*10000);
    int pos = 0;
    PreOrder(root, res, &pos);
    *returnSize = pos;
    return res;
}
// C++
class Solution {
public:
    void PreOrder(const Node* root, vector<int> & res) {
        if(!root) return;
        res.emplace_back(root->val);
        for(auto &ch : root->children)
            PreOrder(ch, res);
    }

    vector<int> preorder(Node* root) {
        vector<int> res;
        PreOrder(root, res);
        return res;
    }
};

 

590. N 叉树的后序遍历 - 力扣(LeetCode)

void PostOrder(const struct Node* root, int* res, int* pos) {
    if(!root) return;
    for(int i = 0; i < root->numChildren; i++)
        PostOrder(root->children[i], res, pos);
    res[(*pos)++] = root->val;
}
int* postorder(struct Node* root, int* returnSize) {
    int *res = (int*)malloc(sizeof(int) * 10000);
    int pos = 0;
    PostOrder(root, res, &pos);
    *returnSize = pos;
    return res;
}
class Solution {
public:
    void PostOrder(const Node* root, vector<int> & res) {
        if(!root) return;
        for(auto &ch : root->children) {
            PostOrder(ch, res);
        }
        res.emplace_back(root->val);
    }

    vector<int> postorder(Node* root) {
        vector<int> res;
        PostOrder(root, res);
        return res;
    }
};

 

429. N 叉树的层序遍历 - 力扣(LeetCode)

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        if(!root) return {};
        vector<vector<int>> ans;
        queue<Node*> q;
        q.push(root);
        while(!q.empty()) {
            vector<int> level;
            for(int i=q.size(); i; --i) {
                Node* cur = q.front();
                q.pop();
                level.push_back(cur->val);
                for(Node* child: cur->children) {
                    q.push(child);
                }
            }
            ans.push_back(move(level));
        }
        return ans;
    }
};
int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {
    int **ans = (int **)malloc(sizeof(int *) * 1000);
    *returnColumnSizes = (int *)malloc(sizeof(int) * 1000);
    if (!root) {
        *returnSize = 0;
        return ans;
    }
    struct Node **queue = (struct Node**)malloc(sizeof(struct Node*) * 10000);
    int head = 0, tail = 0, level = 0;
    queue[tail++] = root;
    while (head != tail) {
        int cnt = tail - head;
        ans[level] = (int *)malloc(sizeof(int) * cnt);
        for (int i = 0; i < cnt; ++i) {
            struct Node * cur = queue[head++];
            ans[level][i] = cur->val;
            for (int j = 0; j < cur->numChildren; j++) {
                queue[tail++] = cur->children[j];
            }
        }
        (*returnColumnSizes)[level++] = cnt;
    }
    *returnSize = level;
    free(queue);
    return ans;
}

 

559. N 叉树的最大深度 - 力扣(LeetCode)

int Max(int a, int b){
    return a > b ? a : b;
}
int maxDepth(struct Node* root) {
    if(root == NULL) return 0;
    int depth = 0;
    for(int i = 0; i < root->numChildren; i++){
        depth = Max(depth, maxDepth(root->children[i]));
    }
    return depth + 1;
}
class Solution {
public:
    int maxDepth(Node* root) {
        if(!root) return 0;
        int depth = 0;
        for(auto child : root->children) {
            depth = max(depth, maxDepth(child));
        }
        return depth + 1;
    }
};

 

posted @ 2026-10-02 07:03  董晓  阅读(7)  评论(0)    收藏  举报