| | | 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 | | using LeetCode.Core.Exceptions; |
| | | 15 | | |
| | | 16 | | namespace LeetCode.Core.Models; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Definition for a binary tree node |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class TreeNode |
| | | 22 | | { |
| | | 23 | | public TreeNode? left; |
| | | 24 | | |
| | | 25 | | public TreeNode? right; |
| | | 26 | | |
| | | 27 | | public int val; |
| | | 28 | | |
| | 2836 | 29 | | public TreeNode(int? val = null, TreeNode? left = null, TreeNode? right = null) |
| | 2836 | 30 | | { |
| | 2836 | 31 | | this.left = left; |
| | 2836 | 32 | | this.right = right; |
| | 2836 | 33 | | this.val = val ?? 0; |
| | 2836 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static TreeNode ToTreeNodeOrThrow(IEnumerable<int?> values) |
| | 74 | 37 | | { |
| | 74 | 38 | | return ToTreeNode(values) ?? throw new TreeNodeBuildException(); |
| | 74 | 39 | | } |
| | | 40 | | |
| | | 41 | | public static TreeNode? ToTreeNode(IEnumerable<int?> values) |
| | 566 | 42 | | { |
| | 566 | 43 | | using var enumerator = values.GetEnumerator(); |
| | | 44 | | |
| | 566 | 45 | | if (!enumerator.MoveNext() || enumerator.Current == null) |
| | 66 | 46 | | { |
| | 66 | 47 | | return null; |
| | | 48 | | } |
| | | 49 | | |
| | 500 | 50 | | var root = new TreeNode(enumerator.Current.Value); |
| | 500 | 51 | | var queue = new Queue<TreeNode>(); |
| | | 52 | | |
| | 500 | 53 | | queue.Enqueue(root); |
| | | 54 | | |
| | 1918 | 55 | | while (queue.Count > 0) |
| | 1918 | 56 | | { |
| | 1918 | 57 | | var current = queue.Dequeue(); |
| | | 58 | | |
| | 1918 | 59 | | if (enumerator.MoveNext()) |
| | 1533 | 60 | | { |
| | 1533 | 61 | | if (enumerator.Current.HasValue) |
| | 1164 | 62 | | { |
| | 1164 | 63 | | current.left = new TreeNode(enumerator.Current.Value); |
| | | 64 | | |
| | 1164 | 65 | | queue.Enqueue(current.left); |
| | 1164 | 66 | | } |
| | 1533 | 67 | | } |
| | | 68 | | else |
| | 385 | 69 | | { |
| | 385 | 70 | | break; |
| | | 71 | | } |
| | | 72 | | |
| | 1533 | 73 | | if (enumerator.MoveNext()) |
| | 1418 | 74 | | { |
| | 1418 | 75 | | if (!enumerator.Current.HasValue) |
| | 368 | 76 | | { |
| | 368 | 77 | | continue; |
| | | 78 | | } |
| | | 79 | | |
| | 1050 | 80 | | current.right = new TreeNode(enumerator.Current.Value); |
| | | 81 | | |
| | 1050 | 82 | | queue.Enqueue(current.right); |
| | 1050 | 83 | | } |
| | | 84 | | else |
| | 115 | 85 | | { |
| | 115 | 86 | | break; |
| | | 87 | | } |
| | 1050 | 88 | | } |
| | | 89 | | |
| | 500 | 90 | | return root; |
| | 566 | 91 | | } |
| | | 92 | | } |