N叉树的前序遍历和后续遍历

N叉树的前序遍历和后续遍历

题目链接:

589. N 叉树的前序遍历(简单)

590. N 叉树的后序遍历(简单)

题解

思路:可以用“二叉树的统一迭代遍历 ”解决

代码(C++):

//N叉树的前序遍历(迭代法)(中左右——右左中null)
class Solution {
public:
    vector<int> preorder(Node* root) {
        stack<Node*> sta;
        vector<int> result;
        if (root != nullptr) sta.push(root);
        while (!sta.empty()) {
            Node* node = sta.top();
            if (node != nullptr) {
                sta.pop();
                int size = node->children.size();
                for (int i = size - 1; i >= 0; i--) {
                    sta.push(node->children[i]);
                }
                sta.push(node);
                sta.push(nullptr);
            } else {
                sta.pop();
                node = sta.top();
                sta.pop();
                result.push_back(node->val);
            }
        }
        return result;
    }
};
​
//N叉树的后序遍历(迭代法)(左右中——中null右左)
class Solution {
public:
    vector<int> postorder(Node* root) {
        stack<Node*> sta;
        vector<int> result;
        if (root != nullptr) sta.push(root);
        while (!sta.empty()) {
            Node* node = sta.top();
            if (node != nullptr) {
                sta.push(nullptr);
                int size = node->children.size();
                for (int i = size - 1; i >= 0; i--) {
                    sta.push(node->children[i]);
                }
            } else {
                sta.pop();
                node = sta.top();
                sta.pop();
                result.push_back(node->val);
            }
        }
        return result;
    }
};

代码(Java):

//N叉树的前序遍历(迭代法)(中左右——右左中null)
class Solution {
    public List<Integer> preorder(Node root) {
        Stack<Node> sta = new Stack<>();
        List<Integer> result = new ArrayList<>();
        if (root != null) sta.push(root);
        while (!sta.empty()) {
            Node node = sta.peek();
            if (node != null) {
                sta.pop();
                int size = node.children.size();
                for (int i = size - 1; i >= 0; i--) {
                    sta.push(node.children.get(i));
                }
                sta.push(node);
                sta.push(null);
            } else {
                sta.pop();
                node = sta.peek();
                sta.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}
​
//N叉树的后序遍历(迭代法)(左右中——中null右左)
class Solution {
    public List<Integer> postorder(Node root) {
        Stack<Node> sta = new Stack<>();
        List<Integer> result = new ArrayList<>();
        if (root != null) sta.push(root);
        while (!sta.empty()) {
            Node node = sta.peek();
            if (node != null) {
                sta.push(null);
                int size = node.children.size();
                for (int i = size - 1; i >= 0; i--) {
                    sta.push(node.children.get(i));
                }
            } else {
                sta.pop();
                node = sta.peek();
                sta.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

分析:

  • 时间复杂度:O(N),遍历树中的每一个节点。

  • 空间复杂度:O(N),主要是栈的开销,栈中的元素不会超过N。

posted @ 2021-11-30 09:21  wltree  阅读(112)  评论(0编辑  收藏  举报