Leetcode刷题笔记题解(C++):590. N 叉树的后序遍历

Leetcode刷题笔记题解(C++):590. N 叉树的后序遍历_第1张图片

思路:类似于二叉树的排序,这里需要将子树进行依次递归遍历,前序遍历也与之类似

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    //用于保存结果
    vector res;
    vector postorder(Node* root) {
        helper(root);
        return res;
    }
    //递归函数
    void helper(Node* root){
        //如果节点为空
        if(root == nullptr) return;
        //获取当前节点的子节点
        vector children = root->children;
        //依次递归调用叶子节点
        for(int i = 0;ival);
    }
};

你可能感兴趣的:(Leetcode算法题解,leetcode,笔记,c++)