zoukankan      html  css  js  c++  java
  • Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历

    给定一个 N 叉树,返回其节点值的前序遍历。

    class Node {
    public:
        int val;
        vector<Node*> children;
    
        Node() {}
    
        Node(int _val, vector<Node*> _children) {
            val = _val;
            children = _children;
        }
    };
    
    //递归
    class Solution {
    public:
        vector<int> res;
        vector<int> preorder(Node* root) {
            if(root == NULL)
                return res;
            GetAns(root);
            return res;
        }
    
        void GetAns(Node* root)
        {
            if(root == NULL)
                return;
            res.push_back(root ->val);
            int len = root ->children.size();
            for(int i = 0; i < len; i++)
            {
                GetAns(root ->children[i]);
            }
        }
    };
  • 相关阅读:
    bzoj 1076
    CF1000G
    CF979E
    bzoj 3129
    CF451E
    CF939F
    CF1065D
    CF1015F
    Enum与最佳単例设计
    悲观锁 vs 乐观锁 vs Redis
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10434061.html
Copyright © 2011-2022 走看看