| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 a binary tree node |
| | 18 | | /// </summary> |
| | 19 | | public class TreeNode |
| | 20 | | { |
| | 21 | | public TreeNode? left; |
| | 22 | |
|
| | 23 | | public TreeNode? right; |
| | 24 | |
|
| | 25 | | public int val; |
| | 26 | |
|
| 2789 | 27 | | public TreeNode(int? val = null, TreeNode? left = null, TreeNode? right = null) |
| 2789 | 28 | | { |
| 2789 | 29 | | this.left = left; |
| 2789 | 30 | | this.right = right; |
| 2789 | 31 | | this.val = val ?? 0; |
| 2789 | 32 | | } |
| | 33 | |
|
| | 34 | | public static TreeNode? ToTreeNode(IEnumerable<int?> values) |
| 556 | 35 | | { |
| 556 | 36 | | using var enumerator = values.GetEnumerator(); |
| | 37 | |
|
| 556 | 38 | | if (!enumerator.MoveNext() || enumerator.Current == null) |
| 66 | 39 | | { |
| 66 | 40 | | return null; |
| | 41 | | } |
| | 42 | |
|
| 490 | 43 | | var root = new TreeNode(enumerator.Current.Value); |
| 490 | 44 | | var queue = new Queue<TreeNode>(); |
| | 45 | |
|
| 490 | 46 | | queue.Enqueue(root); |
| | 47 | |
|
| 1882 | 48 | | while (queue.Count > 0) |
| 1882 | 49 | | { |
| 1882 | 50 | | var current = queue.Dequeue(); |
| | 51 | |
|
| 1882 | 52 | | if (enumerator.MoveNext()) |
| 1506 | 53 | | { |
| 1506 | 54 | | if (enumerator.Current.HasValue) |
| 1146 | 55 | | { |
| 1146 | 56 | | current.left = new TreeNode(enumerator.Current.Value); |
| | 57 | |
|
| 1146 | 58 | | queue.Enqueue(current.left); |
| 1146 | 59 | | } |
| 1506 | 60 | | } |
| | 61 | | else |
| 376 | 62 | | { |
| 376 | 63 | | break; |
| | 64 | | } |
| | 65 | |
|
| 1506 | 66 | | if (enumerator.MoveNext()) |
| 1392 | 67 | | { |
| 1392 | 68 | | if (!enumerator.Current.HasValue) |
| 361 | 69 | | { |
| 361 | 70 | | continue; |
| | 71 | | } |
| | 72 | |
|
| 1031 | 73 | | current.right = new TreeNode(enumerator.Current.Value); |
| | 74 | |
|
| 1031 | 75 | | queue.Enqueue(current.right); |
| 1031 | 76 | | } |
| | 77 | | else |
| 114 | 78 | | { |
| 114 | 79 | | break; |
| | 80 | | } |
| 1031 | 81 | | } |
| | 82 | |
|
| 490 | 83 | | return root; |
| 556 | 84 | | } |
| | 85 | | } |