• 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。

  • 相关阅读:
    Jmeter响应断言的处理。
    Jmeter超时处理。
    HTTP协议简介以及特点。
    自动化测试面试技巧。
    父类构造方法有无参数对子类的影响。
    自动化分层思想分析1.
    设计模式
    遍历课程列表代码。
    如何遍历当前页课程定位分析,以及代码编写。
    “笨方法”学习Python笔记(1)-Windows下的准备
  • 原文地址:https://www.cnblogs.com/wltree/p/15622751.html
Copyright © 2020-2023  润新知