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。

  • 相关阅读:
    C# Array.Sort 省内排序
    Centos7开机启动tomcat8
    使用GeoWebCache发布ArcGIS切片地图(实现高清电子地图)
    获取经纬度之间距离的Java工具类
    centos7上安装rar解压软件
    GeoServer之发布Geotiff存在的问题
    $GPRMC解析
    如何在IDEA单元测试中使用Scanner获取输入内容
    GeoServer修改使用内存
    Github无法访问解决办法
  • 原文地址:https://www.cnblogs.com/wltree/p/15622751.html
Copyright © 2011-2022 走看看