Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of preorder traversal of N-ary trees of leetcode trees

Shulou Source: shulou.com Published: 2022-06-01 13:12:30 09月29日 Update

Editor to share with you how to solve the leetcode tree N-tree preorder traversal problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Given an N-tree, the topic returns the preorder traversal of its node values. For example, given a ternary tree:! [] (https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/narytreeexample.png) returns its preorder traversal: [1mem3pence5re6jp2jp4]. Description: the recursive method is very simple, can you use the iterative method to complete this problem? Source: LeetCode link: https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer / * / / Definition for a Node.class Node {public int val; public List children; public Node () {} public Node (int _ val) {val = _ val;} public Node (int _ val, List _ children) {val = _ val; children = _ children;}}; * / class Solution {List result = new ArrayList () Public List preorder (Node root) {if (root = = null) {return result;} result.add (root.val); for (Node child:root.children) {preorder (child);} return result }} the above is all the contents of this article entitled "how to solve the preorder traversal problem of N-ary trees of leetcode trees". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Articles questions content not much provenance business most official more sources knowledge networks nodes works copyright industry information information channels iterative methods links Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL NVidia Shulou Tech Info Xiaomi