zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
  • 原文地址:https://www.cnblogs.com/wltree/p/15622751.html
Copyright © 2011-2022 走看看