Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历
生活随笔
收集整理的這篇文章主要介紹了
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]);}} };?
轉載于:https://www.cnblogs.com/lMonster81/p/10434061.html
總結
以上是生活随笔為你收集整理的Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到自己男友和闺蜜在一起代表什么
- 下一篇: day 04 作业 循环和分之