| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | // ReSharper disable InconsistentNaming |
| | | 13 | | |
| | | 14 | | namespace LeetCode.Core.Models; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Definition for an n-ary tree node |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class Node |
| | | 20 | | { |
| | | 21 | | public IList<Node>? children; |
| | | 22 | | |
| | | 23 | | public int val; |
| | | 24 | | |
| | 3 | 25 | | public Node() : this(0) { } |
| | | 26 | | |
| | 164 | 27 | | public Node(int? val = null, IList<Node>? children = null) |
| | 164 | 28 | | { |
| | 164 | 29 | | this.children = children; |
| | 164 | 30 | | this.val = val ?? 0; |
| | 164 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static Node? ToNode(int?[] values) |
| | 22 | 34 | | { |
| | 22 | 35 | | if (values.Length == 0 || values[0] == null) |
| | 6 | 36 | | { |
| | 6 | 37 | | return null; |
| | | 38 | | } |
| | | 39 | | |
| | 16 | 40 | | var root = new Node(values[0]); |
| | | 41 | | |
| | 16 | 42 | | var queue = new Queue<Node>(); |
| | | 43 | | |
| | 16 | 44 | | queue.Enqueue(root); |
| | | 45 | | |
| | 16 | 46 | | var i = 2; |
| | | 47 | | |
| | 120 | 48 | | while (queue.Count > 0 && i < values.Length) |
| | 104 | 49 | | { |
| | 104 | 50 | | var current = queue.Dequeue(); |
| | | 51 | | |
| | 104 | 52 | | current.children = []; |
| | | 53 | | |
| | 248 | 54 | | while (i < values.Length && values[i] != null) |
| | 144 | 55 | | { |
| | 144 | 56 | | var child = new Node(values[i]); |
| | | 57 | | |
| | 144 | 58 | | current.children.Add(child); |
| | | 59 | | |
| | 144 | 60 | | queue.Enqueue(child); |
| | | 61 | | |
| | 144 | 62 | | i++; |
| | 144 | 63 | | } |
| | | 64 | | |
| | 104 | 65 | | i++; |
| | 104 | 66 | | } |
| | | 67 | | |
| | 16 | 68 | | return root; |
| | 22 | 69 | | } |
| | | 70 | | } |